footerCallback: sum of pageTotal and total gives the same amount
footerCallback: sum of pageTotal and total gives the same amount
Brecht2727
Posts: 28Questions: 4Answers: 0
Hi all,
The total and pageTotal values gives me the same amount. Any idea why?
footerCallback: function (row, data, start, end, display) {
let api = this.api();
// Remove the formatting to get integer data for summation
let intVal = function (i) {
return typeof i === 'string'
? i.replace(/[\$,]/g, '') * 1
: typeof i === 'number'
? i
: 0;
};
// Total over all pages
total = api
.column(13)
.data()
.reduce((a, b) => intVal(a) + intVal(b), 0);
// Total over this page
pageTotal = api
.column(13, { page: 'current' })
.data()
.reduce((a, b) => intVal(a) + intVal(b), 0);
// Update footer
api.column(1).footer().innerHTML = 'Page total: ' + pageTotal + ' € ('+ total + ' € total over all pages)';
}
This question has an accepted answers - jump to answer
Answers
The only reason I can think of is that you are using server side processing. In this case the only data available for
column().data()
are the rows on the page as these are to only rows at the client. Do you haveserverSide: true
?Kevin
Yes, i am using server side processing. Data comes over ajax.
The amount of pageTotal is correct but the total amount is the same and not the sum over all pages.
Is there a solution?
You will need to calculate the total in your server side script and add the total to the JSON response for each draw. The standard response parameters are documented here. For example add something like
totalSum
to the response:In the
footerCallback
you can useajax.json()
to get the last JSON response to fetch thetotalSum
.Kevin
Hi Kevin,
I have created a separate query that will calculate the total sum outside my foreach loop in the server side script. Below is the response code:
And the footerCallback is as below:
Now all working fine. Many thanks!