Calculate the Total of Every Other Row in a Row Group

Calculate the Total of Every Other Row in a Row Group

sjweathysjweathy Posts: 3Questions: 1Answers: 0

Link to test case (Using a simplified data set): http://live.datatables.net/zuzaxute/1/edit
Debugger code (debug.datatables.net): https://debug.datatables.net/idudix
Error messages shown: jquery.min.js:2 jQuery.Deferred exception: rows is not a function TypeError: rows is not a function
Description of problem: Within my rowGrouping, I have an editable row and then a non-editable reference row right below it and a group sum at the end. I only want to sum the editable rows, which have a tr class of ".override" (they also happen to be the odd rows if that's easier to test). However, I keep getting the error message row/rows is not a function. Selecting the correct rows outside of the rowGrouping works perfectly, but I can't get it to work inside of the rowGrouping. Here's the code I'm using, everything works except when I try and apply the filter section. My goal is just to ignore the reference rows in the group sum.

            var table = $('#myTable').dataTable({
                rowGroup: {
                    startRender: null,
                    endRender: function (rows, group) {

                        //Need to calculate the total of every other row;
                        var catSum = rows
                            .filter(function (rowIdx) {
                                return $(rows(rowIdx).node()).hasClass('.override') ? true:false;
                            })
                            .data()
                            .pluck(15)
                            .reduce(function (a, b) {
                                return a + b.replace(/[^\d]/g, '') * 1;
                            }, 0);
                        catSum = $.fn.dataTable.render.number(',', '.', 0, '$').display(catSum);

                        return $('<tr/>')
                            .append('<td colspan="15">Sum of ' + group + '</td>')
                            .append('<td>' + catSum + '</td>');
                    },
                    dataSrc: 0
                },
                lengthMenu: [[100, 250, 500, -1], [100, 250, 500, "All"]],
                ordering: false,
                scrollY: '50vh',
                scrollCollapse: true,
            });
        });

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736
    Answer ✓

    I think you are using the filter() API incorrectly. The first parameter passed into the function is the value of the cell. I think you will better served by using rows().every(), like this:
    http://live.datatables.net/zuzaxute/3/edit

    Kevin

  • sjweathysjweathy Posts: 3Questions: 1Answers: 0

    @kthorngren Thank you so much! This solved my problem.

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    Answer ✓

    Just to add a little note - it would probably work if you did rows.flatten().filter(...) as the rows() result is a 2D array (to account for the fact that the API allows operations over multiple tables).

    Good to hear that it is working now though!

    Allan

  • sjweathysjweathy Posts: 3Questions: 1Answers: 0

    Thank you for the additional info @allan! I'll probably be using it soon - haha.

Sign In or Register to comment.