datatable中列求和的方法,在DataTables中对过滤的列进行求和

I'm trying to sum the results of filtered columns in DataTables. I've viewed the questions asked on their website and folks have had success using this method.

However, similar code for me produces 'Uncaught TypeError: undefined is not a function."

data = table._('td:nth-child(10)', {"filter": "applied"});

where 'table' is:

var table = $('#dataTable').DataTable({

// my initialization data

});

解决方案

_ (the underscore function) seems to be deprecated in dataTables 1.10.x. In theory it should work with $('#dataTable').dataTable() (the old constructor) but this does not give the expected result (as least not for me).

jQuery.fn.dataTable.Api.register( 'sum()', function () {

return this.flatten().reduce( function ( a, b ) {

return (a*1) + (b*1); // cast values in-case they are strings

});

});

var table = $("#example").DataTable();

$("#example").on('search.dt', function() {

console.log(table.column( 0, {page:'current'} ).data().sum() );

});

would give the same functionality in dataTables 1.10.x as you want in the question header.

table.column( 0, {"filter": "applied"} ).data().sum() works perfectly well also.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值