【发布时间】:2017-02-01 15:34:25
【问题描述】:
我尝试获取每列的总和并在页脚中显示结果。我正在使用 Datatables 提供的 "footerCallback" 函数。但是它没有在页脚中显示任何内容
数据表解释
"注意如果表格没有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 无效的原因是什么?
【问题讨论】:
标签: javascript jquery html datatable callback