Sum filtered data returns NaN

Sum filtered data returns NaN

birobiro Posts: 12Questions: 7Answers: 0

Hi!
i got my DataTable fully configured and working! (Thanks Allan for the awesome plugin)
i've extended it with a custom column search (a textfield on each column).
i need to perform several math operation on each column (retrieved from a json file...etc)****

there are three cases:
1. no filter, the user changes the display length (no problem)
2. filter is set (1st column is the filter, like "2017-02") and the display length is changed (in a number, less than total rows) No problem
3. filter is set (1st column is the filter, like "2017-02") and the display length is changed to all (or a high number ex 10000) PROBLEM

the operations are performed by "group" of dates. I've named this function 'sumby':

function operation(op, val, i) {
    //OP: is the operation chosen from the json file
   //VAL: is the value (retrived from column().data())
  // I: parameter for the start
i = (i) ? i : 0;
    
    switch (op) {
// other operation cases...
'sumby':
        cc = []
        s = val.reduce(function(a, b, c, d) {
          console.log("ABC",a,b,c);
          console.log("COLUMNS ",mainTable.cell(c,0,{search:'applied'}).data());
          cc.push(mainTable.cell((c + i), 0).data());

          return a + b;
        }, 0);
        //console.log("CC ",$.uniqueSort(cc));
        cc = $.uniqueSort(cc);
        if (cc.length > 1) {
          return "---";
        } else {
          return s;
        }
}
}

i've tried with search applied, filtered and none on cell, but both aren't working...

any help will be apreciated!

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,280Questions: 1Answers: 10,425 Site admin

    The error suggests that the data being summed is non numeric. We'd really need a link to the page showing the issue to be able to offer any help beyond that.

    Allan

  • birobiro Posts: 12Questions: 7Answers: 0

    i can't share the link because i'm using it in a Electron/node.js project.

    i'm trying to log the operation (as you can see from the code)
    when the filter is active and the display length is set to all it logs the original (non filtered) data.

    let's suppose the first column was like

    2017-05
    2017-05
    2017-05
    2017-05
    2017-05
    2017-04
    2017-04
    2017-04
    2017-04
    2017-03
    2017-03
    2017-03
    2017-03
    ....
    

    if i search for "2017-03", during the loop in the columns, it still reads "2017-05"

  • allanallan Posts: 63,280Questions: 1Answers: 10,425 Site admin

    Is that the data you are trying to sum?

    I'm not quite getting it I'm afraid!

    Allan

  • birobiro Posts: 12Questions: 7Answers: 0

    I made some screenshots, to be as clear as i can (sorry for my poor english)

    in the first screen, the "normal" situation-> display length set to 25 (default), no filter set (in the 1st column).
    The orange/green header rows shows up the calculation, orange for the partial (the orange part selected), the green one for the total.

    Screen1

    The second screen shows up all the available filter for that column (for example i choose "17-02")
    Screen2

    Here is the issue.
    If i log the values in the first column, i still got "17-05", and not "17-02", that's why it doesen't perform the operation correctly.
    the operation are based on the value of the first column like "sum all values of the same date"
    Screen3

  • allanallan Posts: 63,280Questions: 1Answers: 10,425 Site admin
    Answer ✓

    You use {search:'applied'} in the console.log statement, but not where you are getting the data to push to the array. Its possible that might be the issue, but I'm not actually clear on what c is, so I'm not certain.

    Are you able to use http://live.datatables.net or JSFiddle to create a test case?

    Thanks,
    Allan

This discussion has been closed.