Javascript 计算jquery数据表中两列的总和
我有jQueryDataTable,我想计算两列的总和。 表的初始化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
$('#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()
);
}
} );