sum renderd column
sum renderd column
hsoft2020
Posts: 16Questions: 6Answers: 0
Hi. I have table that some colums are rendered value. I cant manage to get the sum of that colums.I saw som post in datatable archives aboute it. but I could not figure it out how to use it. someone suggest user cells().render() instead of column().data(). I used this code:
total2 = api
.cells(4)
.render('display')
.reduce(function (a, b) {
return intVal(a) + intVal(b);
}, 0);
$(api.column(4).footer()).html(total2);
and I just get NAN value.
This question has an accepted answers - jump to answer
Answers
That looks like it should work. Do you have the
intVal()
function?You can do some debugging of the reduce() function to see what the values are for
a
andb
. Please provide a link to your page or a test case replicating the issue so we can help debug.https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
It depends on how your numbers are rendered. You would need to modify the intVal function or whatever you want to use in order to get the numbers right
Yep, that should work, so it would suggest there's an issue with your data. We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.
Cheers,
Colin
I do numbers rendering differently depending on the user's language. For some users the thousand separator is a period and they use a decimal comma (e.g. German), other users have commas as thousand separators and decimal points (e.g. English).
This is my code to make rendered numbers numeric depending on the user language:
Worth noting that as of 1.12 the number renderer can do automatic locale formatting.
Allan
Hi. I could provide a test link.
please see this:
https://affilate.buy.parsdatam.company/testdt.php
in Share Amount columt ajax return the share precent and data will be rendered with this formula: share amount= share precent /100* Payment
As you see total sum in columns 2 and 3 works fine because they are not rendered but in column 4 it returns 0
this looks weird:
You want column 4 right? I don't think that cells(4) will give you the values of the cells of column 4. Check the docs please: https://datatables.net/reference/api/cells()
This could work
yes , I want column 4 sum. but as I said column().data() just return raw data sum. I want to sum the rendered data in column 4.
it contains the percent and I have to multiple it in column 3 data and then sum the result in footer
This thread has the same question. As rf1234 points out you need to add
null
to thecells()
API, like this:This means the
row-selector
isnull
meaning all rows and thecolumn-selector
is 4.Kevin
thank. this worked. I missed the row selector and I get wrong result. thanks again for answering.