sum column not working if same value number ?

sum column not working if same value number ?

ardyanto19ardyanto19 Posts: 3Questions: 1Answers: 0
edited October 2019 in Free community support
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.

Answers

  • kthorngrenkthorngren Posts: 21,163Questions: 26Answers: 4,921

    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

  • ardyanto19ardyanto19 Posts: 3Questions: 1Answers: 0
    edited October 2019

    i got same number 0 if i use indexof

    https://jsfiddle.net/6pctbm9r/1/

  • colincolin Posts: 15,237Questions: 1Answers: 2,598

    Hi @ardyanto19 ,

    The problem is this line:

    var cur_index = api.column(idx).data().indexOf(b);
    

    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

  • kthorngrenkthorngren Posts: 21,163Questions: 26Answers: 4,921

    Colin is correct. The other option is to use the data and display parameters of the footerCallback. The data is the table data and the display 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 the position array. Then get the sumCondition value from the data 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

  • ardyanto19ardyanto19 Posts: 3Questions: 1Answers: 0

    Hi @kthorngren & @colin thanks for helping..it's working :) thank you very much

This discussion has been closed.