footerCallback total is not calculating the right column
footerCallback total is not calculating the right column
malhamoud
Posts: 11Questions: 2Answers: 0
Hi,
I'm using footerCallback to add the total amount of selected product, every thing is working fine except getting the total here is my code:
`
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
totalsum = api.column(3)
.data()
.reduce(function (a, b) {
return intVal(a) + intVal(b);
}, 0);
$(api.column(3).footer()).html(
'(' + totalsum + ' SAR) '
);
},
`
the odd thing is that when I change the api.column(3) to api.column(2) - which is not the wonted column- it works fine!
This discussion has been closed.
Answers
The column numbers start at 0.
api.column(3)
is the forth column. If this doesn't help then we will need to see an example of the problem in order to help. Please post a link to your page or a test case replicating the issue.https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
I tried to but because I am binding data from an api to generate the table content I could not figure out how to use codepin or http://live.datatables.net/.
however;
I am binding the quantity of and multiply it with the price and after that I try to sum the results in one field.
it seems to me it is starting to sum before the multiply because when I change the column the sum works fine
You can take an example of your data and use
data
to feed it into Datatables, like this example. Without seeing what you are doing it will be impossible to provide suggestions.Kevin
I tried but its not working
http://live.datatables.net/giponiyi/1/
You have a lot of syntax errors to work through. However I may be able to provide a starting point:
You have:
Take a look at the
columns.render
docs and you will see that there are other parameters for the function;( data, type, row, meta )
. Instead of trying to use global variables to store the data from different columns you can use therow
parameter to access and column data for that row. Your code should look more like this:To make your test case run you will need to fix the syntax and typo errors. Plus you will need to add the jQuery and Datatables JS and CSS includes.
Kevin
Thank you very much, I will try to fix the syntax error and git back to you.
I tried to fix the errors and I did there is no more errors, but its not showing anything and I am unable to test the footerCallback. I even tried to somualte the example.
please take a look:
http://live.datatables.net/giponiyi/3/
You haven't included any libraries. Please provide a test case that demonstrates the issue you want help with.
Colin
Like Colin said in the HTML Tab you removed all the libraries and other code. Take a look at http://live.datatables.net/ to see what you should have. You can remove the
table
that is there but the rest should be there for a standard Datatables environment.Additionally you need to have a
tfoot
in order to have the footerCallback code place the sums in the footer. If you don't nothing will appear. Finally you are trying to sum a column that has rendered data. You can do this but it needs to usecells().render()
instead ofcolumn().data()
. Take a look at Colin's answer and example in this thread to see how to make this change.Kevin
I did add the libraries and add every thing was missing on the html side, but still nothing appears ,
and I tried cells().render(). but I don't know if I am doing it right or not because it shows (the first cell data) instead of sum, I tried adding index to cells, but it didn't work either
http://live.datatables.net/rivugeta/1/
The place to start is to look at the browser's console for errors. The test case is getting these two errors:
This is due to having the JS libraries in the wrong order. After moving jquery.js to the top the next error is this:
You defined 5 columns in the HTML table but only 4 using
columns
. They need to match in number. I removed the last column in HTML.Now you are getting
NaN
in the Subtotal column. The reason is you havesubTotal= row.Price*row.Quantity;
but your data is array based, ie, you haven't definedcolumns.data
. See more info about Datatables data structures here. Changed the code to use array notation:subTotal= row[1]*row[2];
.Looks like I forgot to post the link to the thread with Colin's example.
Applying Colin's example we get this in the footer:
(NaN SAR)
.Using console.log in the
intVal
function we see this output for the values:<p>14</p>
You are wrapping the subtotal in
p
tags:return "<p>" + subTotal + "</p>";
. You don't need to do this so I removed them for the working example:http://live.datatables.net/mixegahu/1/edit
You don't need to use
p
tags when returning the data incolumns.render
. But if you do then you will need to update theintVal
function to remove them so only the numbers remain to be added.Kevin
Thank you very much Kevin you saved my project it works now the issue was in the
<
p> tag thank you again and have a nice day Sir.