Row Group include count of full dataset instead of just rows in current page

Row Group include count of full dataset instead of just rows in current page

bfarkasbfarkas Posts: 181Questions: 48Answers: 0

I'm building a button to toggle rowgrouping on and off, and like the idea of adding the count into the title as shown in this example:
https://datatables.net/extensions/rowgroup/examples/initialisation/endRender.html

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

The trouble is the count is only for what is shown on the current page, not in the full dataset. Is there a good way to change the count to account for the full set?

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,315Questions: 26Answers: 4,948
    Answer ✓

    This example from this thread should do what you want.

    Kevin

  • bfarkasbfarkas Posts: 181Questions: 48Answers: 0

    Thanks Kevin, seems like the right path, I altered for what I am trying but I am getting an error that the group is not defined:

    startRender: function ( rows, groups ) {
                var filteredData = $('#p200table').DataTable()
                  .rows()
                  .data()
                  .filter( function ( data, index ) {
                    return data[4] == group ? true : false;
                  } )
                return group +' ('+filteredData.rows.count()+')';
                },
                dataSrc: 4
    

    I didn't think it needed to be as it is being passed no? I'm sure i am missing something silly here.

  • kthorngrenkthorngren Posts: 21,315Questions: 26Answers: 4,948

    You have startRender: function ( rows, groups ). The return data[4] == group ? true : false; is intended to use the second variable in the parameters. Change groups to group.

    Kevin

  • bfarkasbfarkas Posts: 181Questions: 48Answers: 0

    Ok, so that solved the group issue, but I still can't get to the count for the full dataset of a group instead of what is on a page.

  • kthorngrenkthorngren Posts: 21,315Questions: 26Answers: 4,948

    Are you using server side processing? If so that why because only the data on the page is at the client.

    If not please provide a link to your page or a test case showing the issue.

    Kevin

  • bfarkasbfarkas Posts: 181Questions: 48Answers: 0

    No I am not, I just got it to work actually, one hiccup that I am sorting through. here is where i am:

    startRender: function ( rows, group ) {
                var filteredData = $('#p200table').DataTable()
                  .rows()
                  .data()
                  .filter( function ( data, index ) {
                    return data[ 'nest' ] == group ? true : false;
                  } )
                  .count();
                return group +' (' + filteredData + ')';
                },
                dataSrc: 'nest'
    

    The hiccup is the group for blanks does not work because the group name is different., so it always shows up as 0. Thoughts?

  • kthorngrenkthorngren Posts: 21,315Questions: 26Answers: 4,948

    Not sure what you mean by blanks. Can you provide a test case so we can see your data?

    Kevin

  • bfarkasbfarkas Posts: 181Questions: 48Answers: 0

    The column that is being used for rowgrouping does not require data, so there is always a group of blanks/no data.

  • bfarkasbfarkas Posts: 181Questions: 48Answers: 0

    Trying something like this currently to no avail:

    startRender: function ( rows, group ) {
                var filteredData = $('#p200table').DataTable()
                  .rows()
                  .data()
                  .filter( function ( data, index ) {
                     if( data[ 'nest' ] == "" ) {
                         return data[ 'nest' ] == "" ? true : false;
                    }
                  else {
                    return data[ 'nest' ] == group ? true : false;
                } })
                  .count();
                return group +' (' + filteredData + ' Prospects)';
                },
                dataSrc: 'nest'
    
  • kthorngrenkthorngren Posts: 21,315Questions: 26Answers: 4,948

    All I can suggest is use console.log statements or the browser's debugger to see what the value of group is for the empty group and data[ 'nest' ]. You may need to change this code to handle that situation:

                  .filter( function ( data, index ) {
                    return data[ 'nest' ] == group ? true : false;
                  } )
    

    Kevin

  • kthorngrenkthorngren Posts: 21,315Questions: 26Answers: 4,948
    edited September 2020

    Looks like we cross posted :smile:

    I added a couple of empty office columns to this example and it works without code changes:
    http://live.datatables.net/piwahega/1/edit

    My above comment still stands. You need to validate what the group and data['nest'] values are when the column is empty.

    Kevin

  • bfarkasbfarkas Posts: 181Questions: 48Answers: 0

    Yeah, I've been doing that, and the blanks don't seem to show any value in the return, as in they aren;t there at all. I have since worked out a conditional for when blank to just not include the number as for how this is being used it does not matter.

    rowGroup: {
                enable: false,
                emptyDataGroup: 'No nest',
                endRender: null,
                startRender: function ( rows, group ) {
                var filteredData = $('#p200table').DataTable()
                  .rows()
                  .data()
                  .filter( function ( data, index ) {
                    return data[ 'nest' ] == group ? true : false;
                  } )
                  .count();
                if( filteredData == 0 ) {
                return group }
                if( filteredData == 1 ) {
                return group +' (' + filteredData + ' prospect)'; }
                else {
                return group +' (' + filteredData + ' prospects)';
                }},
                dataSrc: 'nest'
            },
    
    
This discussion has been closed.