4

我正在尝试对 DataTables 中过滤列的结果求和。我查看了他们网站上提出的问题,人们使用这种方法取得了成功。

但是,对我来说,类似的代码会产生“未捕获的类型错误:未定义不是函数”。

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

其中“表”是:

var table = $('#dataTable').DataTable({
// my initialization data
});
4

1 回答 1

12

_ (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).

But see this -> http://datatables.net/plug-ins/api/sum()

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.

see demo -> http://jsfiddle.net/6qLwkwud/

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

于 2014-10-13T09:55:28.433 回答