Sum multiple columns with footerCallback
Sum multiple columns with footerCallback
btraill
Posts: 2Questions: 1Answers: 0
Hello, hoping someone can help me to understand how I can sum multiple columns using the footerCallback function.
I have this example summing one column (7) -- but I would like it to sum all columns that I specify.
`"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( 7 )
.data()
.reduce( function (a, b) {
return intVal(a) + intVal(b);
}, 0 );
// Total over this page
pageTotal = api
.column( 7, { page: 'current'} )
.data()
.reduce( function (a, b) {
return intVal(a) + intVal(b);
}, 0 );
// Update footer
$( api.column( 7 ).footer() ).html(
'('+ total +')'
);
}`
Any help would be greatly appreciated!
This discussion has been closed.
Answers
I just found something like this which appears to support multiple columns. I was hoping for some thing more dynamic/not statically typed for each but this may suffice.
https://phppot.com/jquery/calculate-sum-total-of-datatables-column-using-footer-callback/