I am having problems with tiny granularity by inserting the sum value of each column with the class "sum" into the footer.
The code (more or less used directly from DataTables.net) goes:
var table = $('#example').DataTable(); table.columns('.sum').each(function (el) { var sum = table .column(el) .data() .reduce(function (a, b) { return parseInt(a, 10) + parseInt(b, 10); }); $(el).html('Sum: ' + sum); });
"sum" has the correct meaning, but is somehow not inserted in the footer! That is, its -element appears empty.
Note that the snapshot below works fine, but I want to sum each column with the class sum.
var table = $('#example').DataTable(); var column = table.column(4); $(column.footer()).html( column.data().reduce(function (a, b) { return parseInt(a, 10) + parseInt(b, 10); }) );
//////////////////////////////////////////////// //////////////////////////////////
EDIT - Workaround:
I moved the code to where the DataTable is initialized, and instead went off with footerCallback. See below code:
"footerCallback": function (row, data, start, end, display) { var api = this.api(), data;
Somehow now the value is inserted into the right tfoot element. I still donβt know why this will not work in the first place (but, as mentioned in the comment, the order of including JS files could be related to some of them).
Now I just need to figure out how to loop multiple columns using class = "sum" ...
javascript jquery jquery-datatables datatables
skepnaden
source share