rowGroup multi level group startRender question

rowGroup multi level group startRender question

ianoel230982ianoel230982 Posts: 12Questions: 4Answers: 0

Hi,

First post here on datatables forum. Love the product, real game changer for my team at work. Anyway, on to my question.

I'm working on a table whereby i am using extension rowGroup with multi level grouping and activating it like so
dataSrc: ["PayrollNo", function (row) {
return moment(row.ExtraDutyFrom).format("DD/MM/YYYY")
}

What i wanted to do was insert some counts of data only into the 2nd group headings, which i found something similar what i wanted here - https://datatables.net/reference/option/rowGroup.startRender

startRender: function ( rows, group ) {
return group +' ('+rows.count()+' rows)';
}

but this puts the count in both the 'PayrollNo' heading and also the date heading. I note the description for startRender says 'The function defined by this option will be called once for every group shown in the DataTables', which i why i get the counts in both headings.
In my case i only want this to run on my second data source (the date returned by the moment function above).

So my question, is it possible to only do the count option on the 2nd group? Secondly, is it then possible to hide any rows on the table whose count returns less than 2?

I'm not in a position to post an example just yet as per forum rules as its only a question of is it possible (however if you want one i'll certainly do the necessary).

Any help appreciated :-)
Thanks.

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,112Questions: 1Answers: 2,583
    Answer ✓

    is it possible to only do the count option on the 2nd group?

    The third arg to rowGroup.startRender is level - this indicates the depth when doing multi-level grouping. You can use that to conditionally return different strings.

    is it then possible to hide any rows on the table whose count returns less than 2?

    Unfortunately not. The extension simply groups rows, so it doesn't remove/hide any. You could call rows().remove() in rowGroup.startRender if the count is less then two and then do a redraw to remove the group. That would remove them, but you'd only get them back if it's an Ajax table and you do an ajax.reload().

    Hope that helps,

    Colin

  • ianoel230982ianoel230982 Posts: 12Questions: 4Answers: 0

    Thanks for that. I did try some conditional logic on the 'level' earlier but didn't get back what I was expecting. I'll have another look tomorrow at work and provide an example if I have some more issues.

    I'll also have a look at removing the rows as well, I think in my instance it would be ok to have them return once my Ajax runs again.

    Thanks again.

  • ianoel230982ianoel230982 Posts: 12Questions: 4Answers: 0

    Managed to get my logic working for returning the different strings on the different groups so thanks for the pointer. Turns out i don't need to remove the rows or hide them now either.

    I went with the following, may not be the most elegant, but seems to work.

    startRender: function (rows, group, level) {
                    if (level === 1) {
                        console.log(rows)
                        return 'Extra duty worked on <strong>' + group + '</strong> (' + rows.count() + ' claims)';
    
                    } else if (level === 0) {
                        return "Payroll No. " + group;
                    }
                },
                dataSrc: ['PayrollNo',
                    function (row) {
                        return moment(row.ExtraDutyFrom).format("DD/MM/YYYY")
                    }
                ]
    

    Thanks again :-)

This discussion has been closed.