rowGroup does not sum values

rowGroup does not sum values

jpavanjpavan Posts: 38Questions: 12Answers: 0

Good afternoon !
I have the following problem when trying to summarize the values ​​of grouped rows.

I leave two examples available, of identical code, except for one line

Example 1: https://pavanconsultores.com.ar/pruebas/pru04a.php

Code:

            var salaryAvg = rows
            .data()
            .pluck('fld.fld_sem_09')
            .reduce( function (a, b) {
                // return a + b.replace(/[^\d]/g, '')*1;
               return a;
            }, 0);

Example 2: https://pavanconsultores.com.ar/pruebas/pru04b.php

Code:

            var salaryAvg = rows
            .data()
            .pluck('fld.fld_sem_09')
            .reduce( function (a, b) {
                return a + b.replace(/[^\d]/g, '')*1;
               // return a;
            }, 0);

Note that the only difference is the comment on line 6 and 7 on both sides.

In example 1 the groups are shown correctly, but they do not sum the totals.
In example 2 the groups and totals are not shown.

Any ideas or help for group totals to be displayed correctly ?

Thank you

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    The second one is giving an error

    pru04b.php:310 Uncaught TypeError: Cannot read property 'replace' of undefined
        at pru04b.php:310
        at s.reduce (<anonymous>)
        at h.endRender (pru04b.php:309)
        at h._groupDisplay (dataTables.rowGroup.min.js:22)
        at h._draw (dataTables.rowGroup.min.js:21)
        at HTMLTableElement.<anonymous> (dataTables.rowGroup.min.js:20)
        at HTMLTableElement.dispatch (jquery-3.3.1.min.js:2)
        at HTMLTableElement.y.handle (jquery-3.3.1.min.js:2)
        at Object.trigger (jquery-3.3.1.min.js:2)
        at HTMLTableElement.<anonymous> (jquery-3.3.1.min.js:2)
    

    So b is undefined, causing b.replace to fail. I suspect that undefined is in your dataset - just do a check for it before doing the replace.

    Colin

  • jpavanjpavan Posts: 38Questions: 12Answers: 0

    Hi Colin, I had noticed, but I don't understand how I should solve it.
    If I look at the documentation I used as a guide, I don't find that reference:

    https://datatables.net/extensions/rowgroup/examples/initialisation/customRow.html

    Could you give me some other detail?

    Thank you !

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    I just took another look and I think you've found a bug - it seems pluck() doesn't like it when there's a nested object - there's a simple example of that here. I've raised it internally (DD-1386 for my reference) and we'll report back here when there's an update.

    In the meantime, you can do this without pluck(), and just reduce(). Something like this:

    table.rows().data().reduce(function(a, b) {
        return b.fld.fld_sem_09 === null? a : a + b.fld.fld_sem_09.toString().replace(/[^\d]/g, '') * 1;
    }, 0)
    

    Colin

This discussion has been closed.