简体   繁体   中英

calculate sum of two columns inside jquery datatables

I have jquery datatable and I want to calculate sum of two columns. initialization of the table

 $('#myTable').DataTable({
        autoWidth: false,
        searching: false,
    ...
     initComplete: function (settings, json) {
        // calculate sum and inject into second and third row in the footer
     });
  });

<table id="myTable">
   <thead>
      <tr>
         <th>Id</th>
         <th>Time one</th>
         <th>Time two</th>
      </tr>
   </thead>
   <tfoot>
      <tr>
         <th></th>
         <th></th>
         <th></th>
      </tr>
   </thead>
</table>

Update:

vales inside this columns are like:

=====================
| 1 | 01:15 | 10:00 |
=====================
| 9 | 11:44 | 0:120 

|

You can do it using DataTable Plugin API:

// Simply get the sum of a column
var table = $('#example').DataTable();
table.column( 3 ).data().sum();

// Insert the sum of a column into the columns footer, for the visible
  // data on each draw
  $('#example').DataTable( {
    drawCallback: function () {
      var api = this.api();
      $( api.table().footer() ).html(
        api.column( 4, {page:'current'} ).data().sum()
      );
    }
  } );

More: https://datatables.net/plug-ins/api/sum()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM