javascript - Calculate total for selected items in a table -


i have 3 tables each multiple rows. need find way calculate total of columns. each row has checkbox, when checked, need able add rows values total.

i have this, adding totals each column, can not work out how when checkbox selected total updated, , if deselected removed total.

table example...

<table class="transfer-group-table table table-hover tablesorter"> <thead>     <tr>         <th>name</th>         <th>invoice #</th>         <th>invoice amount</th>         <th>order status</th>         <th>payment menthod</th>         <th>service fee</th>         <th>funding fee</th>         <th>delivery date</th>         <th>transfer amount</th>         <th></th>     </tr> </thead> <tbody>     <tr id="2942">          <td>             <p>a company ltd</p>         </td>         <td>             <p>18602</p>         </td>         <td class="amountloaned">             <p>324.00</p>         </td>         <td>             <p>completed </p>         </td>         <td>             <p>bacs</p>         </td>         <td class="servicecharge">             <p>0.04</p>         </td>         <td class="feeamount">             <p>4.54</p>         </td>         <td>             <p>may 29, 2015</p>         </td>         <td class="transferamount">             <p>2.50</p>         </td>         <td>             <input type="checkbox" class="totalamountcb">         </td>     </tr> </tbody> 

javascript...

// calculate total invoice amount selected items function calculateinvoicetotals() { var sum = 0; // iterate through each td based on class , add values $(".amountloaned").each(function () {      var value = $(this).text();     // add if value number     if (!isnan(value) && value.length != 0) {         sum += parsefloat(value);     } }); $('#totalinvoiceamt').text(sum.tofixed(2)); }; // calculate total transfer amount selected items function calculatetransfertotals() { var sum = 0; $(".transferamount").each(function () {      var value = $(this).text();     // add if value number     if (!isnan(value) && value.length != 0) {         sum += parsefloat(value);     } }); $('#totaltransferamt').text(sum.tofixed(2)); }; 

traverse using $.fn.closest() tr $.fn.find() checkbox using $.fn.is() can check whether checkbox checked or not.

if($(this).closest('tr').find(':checkbox').is(':checked')){     //perform addition  } 

complete code

// calculate total invoice amount selected items function calculateinvoicetotals() {     var sum = 0;     // iterate through each td based on class , add values     $(".amountloaned").each(function() {         //check if checkbox checked         if ($(this).closest('tr').find(':checkbox').is(':checked')) {             var value = $(this).text();             // add if value number             if (!isnan(value) && value.length != 0) {                 sum += parsefloat(value);             }         }     });     $('#totalinvoiceamt').text(sum.tofixed(2)); }; // calculate total transfer amount selected items function calculatetransfertotals() {     var sum = 0;     $(".transferamount").each(function() {         //check if checkbox checked         if ($(this).closest('tr').find(':checkbox').is(':checked')) {             var value = $(this).text();             // add if value number             if (!isnan(value) && value.length != 0) {                 sum += parsefloat(value);             }         }     });     $('#totaltransferamt').text(sum.tofixed(2)); }; 

demo


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -