计算jquery数据表中两列的总和

时间:2017-01-18 12:24:19

标签: javascript jquery datatables

我有jquery数据表,我想计算两列的总和。 表的初始化

 $('#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>

更新

此列中的值如下:

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

|

1 个答案:

答案 0 :(得分:0)

您可以使用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()
      );
    }
  } );

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