我尝试获取每列的总和并将结果显示在页脚中。我在用着“页脚回调” https://datatables.net/reference/option/footerCallbackDatatables提供的功能。但是它在页脚中没有显示任何内容
数据表解释
“请注意,如果表格没有 tfoot 元素,则此回调
不会被解雇。”
所以我已将 tfoot 添加到表中,以便回调将被触发
<table id="monthlytable" class="display" cellspacing="0" width="100%">
<thead></thead><tfoot></tfoot></table>
回调函数:
"footerCallback": function ( tfoot, data, start, end, display ) {
var api = this.api(), data;
// Remove the formatting to get integer data for summation
var intVal = function ( i ) {
return typeof i === 'string' ?
i.replace(/[\$,]/g, '')*1 :
typeof i === 'number' ?
i : 0;
};
// Total over all pages
total = api
.column( 3 )
.data()
.reduce( function (a, b) {
return intVal(a) + intVal(b);
}, 0 );
// Update footer
var numFormat = $.fn.dataTable.render.number( '\,', '.', 2, '£' ).display;
$( api.column( 3 ).footer() ).html(numFormat(total));
}
我尝试过使用"headerCallback"
使用与上面相同的代码(更改为显示在标题中)并且它工作得很好。
headerCallback 有效但 footerCallback 无效是否有原因?