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 more than 1 column?
I thought I might need to add more th
tags for the columns I want to sum:
<tfoot>
<tr>
<th colspan="4" style="text-align:right">Total:</th>
<th></th>
</tr>
</tfoot>
And add another callback function per each column, but so far I have had no joy. Can anyone advise?
"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
total = api
.column( 4 )
.data()
.reduce( function (a, b) {
return intVal(a) + intVal(b);
}, 0 );
// Total over this page
pageTotal = api
.column( 4, { page: 'current'} )
.data()
.reduce( function (a, b) {
return intVal(a) + intVal(b);
}, 0 );
// Update footer
$( api.column( 4 ).footer() ).html(
'$'+pageTotal +' ( $'+ total +' total)'
);
}
NOTE: I might need another column in the table/dataset with numbers that I can sum on.
Changed the table so there is 2 columns that can be summed FIDDLE.
In this fiddle it is working in col 5 but how do I get it to work in col 3?
// Total over all pages
,// Total over this page
,// Update footer
steps for index 3 also. – Santhosh Nayak