Summing a filtered column in DataTables

痴心易碎 提交于 2019-12-10 18:43:25

问题


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
});

回答1:


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



来源:https://stackoverflow.com/questions/26307622/summing-a-filtered-column-in-datatables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!