sum column not working if same value number ?
sum column not working if same value number ?

var pri_tm_valid = api
.column(3)
.data()
.reduce(function (a, b) {
var cur_index = api.column(3).data().indexOf(b);
if (api.column(1).data()[cur_index] == "VALID") {
console.log(intVal(a), intVal(b), parseFloat(a), parseFloat(b), a, b);
// return intVal(a) + intVal(b);
var x = parseFloat(a) || 0;
var y = parseFloat(b) || 0;
return x + y;
} else {
return parseInt(a);
}
}, 0);
$('tr:eq(1) th:eq(3)', api.table().footer()).html(pri_tm_valid);
i have row with value 12, 14, 12, 16
but when i sum the summary the value is 30.. why not 54 ?
please help me..thanks
Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.
This discussion has been closed.
Answers
Thats a lot of code to scan to try finding the problem. Please post a link to your page or better a simple test case showing the problem.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
i got same number 0 if i use indexof
https://jsfiddle.net/6pctbm9r/1/
Hi @ardyanto19 ,
The problem is this line:
If you've got the same value multiple times in the column, this will give the index of the first occurrence. If you want to compare different columns, it would be best to use
rows().every()
,Cheers,
Colin
Colin is correct. The other option is to use the
data
anddisplay
parameters of thefooterCallback
. Thedata
is the table data and thedisplay
is an array of indexes in the order the table is displayed. Instead of the above line Colin pointed out you will want to get the index using a counter and theposition
array. Then get thesumCondition
value from thedata
to use for the comparison. Your updated example:https://jsfiddle.net/8vxjmhbr/5/
You will want to test this with more data to make sure it works properly.
Kevin
Hi @kthorngren & @colin thanks for helping..it's working
thank you very much