How to skip blank rows in footer callback average calculation?

How to skip blank rows in footer callback average calculation?

smasonsmason Posts: 25Questions: 11Answers: 0
edited January 2019 in Free community support

If I have a column that has rows that do not have values, how can I skip them in the footer callback? Here's my footer callback so far. But if there is a row that doesn't have a value then I need to skip it.

Right now it prints NaN in the footer if there are blank rows.

//DataTables
        $(".results-table").DataTable({                 
            "aaSorting": [ ],
            "footerCallback": function(row, data, start, end, display) {
                  var api = this.api();

                  // Remove the comma for calcuations
                var intVal = function ( i ) {
                        return typeof i === 'string' ?
                            i.replace(/,/g,'')*1 :
                    typeof i === 'number' ?
                        i : 0;
                    };

                  var columnData = api.column(0).data();

                //Avg
                api.columns('.avg', {
                    page: 'current'
                  }).every(function() {

                var numerator = this
                                .data()
                                .reduce( function(a, b) {
                                   return (intVal(a) + intVal(b));
                                }, 0);

                    var denominator = this.data().length;

                    avg = numerator / denominator;
                    avg = avg.toFixed(1)

                    $(this.footer()).html("" + avg + "%");
                  });

            } //end footerCallback

        }); //end DataTables

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,309Questions: 26Answers: 4,947

    Your code seems to work here:
    http://live.datatables.net/xahepoji/1/edit

    The intVal function should return a 0 for blank data. I would suggest using console.log to help debug your variables to determine where the NaN is coming from. Or you could provide a link to your page or a test case replicating the issue. Its purely a matter of debugging the data you have. Its more a Javascript issue than Datatables but if you provide a test case we will help.

    Kevin

  • smasonsmason Posts: 25Questions: 11Answers: 0

    Okay I figured it out. The "blank" rows actually had a dash symbol in them.
    I removed that and it worked. I'm curious though how would I modify the intVal function to also return 0 for a hyphen - ?

  • kthorngrenkthorngren Posts: 21,309Questions: 26Answers: 4,947

    You would update the regex replace in line 10. See if this does the trick:
    i.replace(/(^-$|,)/g,'')*1 :

    Kevin

  • smasonsmason Posts: 25Questions: 11Answers: 0

    That worked great, hypens are ignored.
    I just realized something though, the denominator calculation still uses records that have blanks because it's using this.data().length;

    What approach would could I take to calculate the denominator based on if this row has a number or if it has a hyphen?

  • kthorngrenkthorngren Posts: 21,309Questions: 26Answers: 4,947
    Answer ✓

    One option is to use -api filter()and-api count()` to count the number of cells in the column that have hyphens. Here is an example:
    http://live.datatables.net/yaluyopo/1/edit

    Kevin

This discussion has been closed.