Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/461.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 计算jquery数据表中两列的总和_Javascript_Jquery_Datatables - Fatal编程技术网

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

Javascript 计算jquery数据表中两列的总和,javascript,jquery,datatables,Javascript,Jquery,Datatables,我有jQueryDataTable,我想计算两列的总和。 表的初始化 $('#myTable').DataTable({ autoWidth: false, searching: false, ... initComplete: function (settings, json) { // calculate sum and inject into second and third row in the footer

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

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

|

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

更多信息:

您的问题是什么?您遇到了什么问题?让我重新表述一下这个问题:例如,我想在第三列的页脚处显示总计。不确定OP是想显示总计,还是想在页脚处显示一个包含逐行sumtotal的新列。因此,您可以使用此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()
      );
    }
  } );