Given this footer_callback datatables example here is my FIDDLE.
This basically sums the total per column for 1 column. Can anyone advise how I can do this for
Check out the fiddle here. I'm using the sum plugin. With this approach, you can just add a column index to an array to total it.
I also added the 'applied' filter so the totals dynamically update when filtering.
$(document).ready(function() {
// SUM PLUGIN
jQuery.fn.dataTable.Api.register( 'sum()', function ( ) {
return this.flatten().reduce( function ( a, b ) {
if ( typeof a === 'string' ) {
a = a.replace(/[^\d.-]/g, '') * 1;
}
if ( typeof b === 'string' ) {
b = b.replace(/[^\d.-]/g, '') * 1;
}
return a + b;
}, 0 );
} );
$('#example').DataTable({
"footerCallback": function () {
var api = this.api(),
columns = [3, 5]; // Add columns here
for (var i = 0; i < columns.length; i++) {
$('tfoot th').eq(columns[i]).html('Total: ' + api.column(columns[i], {filter: 'applied'}).data().sum() + '<br>');
$('tfoot th').eq(columns[i]).append('Page: ' + api.column(columns[i], { filter: 'applied', page: 'current' }).data().sum());
}
}
});
});