I have been reading many jQuery Datatables examples on how to use footerCallback
to sum all rows of a MySQL table, but I can get the total sum of a column, when I'm using records per page filter. In the Footer callback example, they show a demo, that do that, here is the code:
$(document).ready(function() {
$('#example').dataTable( {
"footerCallback": function ( row, data, start, end, display ) {
var api = this.api(), data;
// Remove the formatting to get integer data for summation
var intVal = function ( i ) {
return typeof i === 'string' ?
i.replace(/[\$,]/g, '')*1 :
typeof i === 'number' ?
i : 0;
};
// Total over all pages
data = api.column( 4 ).data();
total = data.length ?
data.reduce( function (a, b) {
return intVal(a) + intVal(b);
} ) :
0;
// Total over this page
data = api.column( 4, { page: 'current'} ).data();
pageTotal = data.length ?
data.reduce( function (a, b) {
return intVal(a) + intVal(b);
} ) :
0;
// Update footer
$( api.column( 4 ).footer() ).html(
'$'+pageTotal +' ( $'+ total +' total)'
);
}
} );
} );
But when I apply in my PHP script, total gives me the same amount of pageTotal, when My records per page filter is on 10, if I change to 50 it gives me a new total, but how in the example, when the filter is on 10, it show the sum of all records in the table. I have been on this for two days, can anybody can give me a hint please?
Total over this page
data = api.column( 4, {filter:'applied'} ).data();
pageTotal = data.length ?
data.reduce( function (a, b) {
return intVal(a) + intVal(b);
} ) :
0;