Counting entries under a grouped row

Counting entries under a grouped row

andrea5917andrea5917 Posts: 1Questions: 1Answers: 0

Hello.

I currently have a table extremely similar to the table found here. https://datatables.net/examples/advanced_init/row_grouping.html
Does anybody know of a way to count the number of entries under each group? In this example, it would counting be the names of the people under the city group.

Answers

  • jr42.gordonjr42.gordon Posts: 305Questions: 2Answers: 49

    In the linked example you provided, add logic to the drawCallback

    api.column(2, {page:'current'} ).data().each( function ( group, i ) {
                    if ( last !== group ) {
                        if (last !== undefined) {
                             groupingCounts[last] = counter;
                        }
                        $(rows).eq( i ).before(
                            '<tr class="group"><td colspan="5">'+group+'</td></tr>'
                        );
     
                        last = group;
                        counter = 1;
                    } else {
                        counter++;
                    }
                } );
    

    Now please note that drawCallback is called after every filter, sort, pagination. So if you only want this logic performed once, say after DataTable inialization and default sort applied to the city column, then see initComplete or simply add it after your .DataTable() call.

This discussion has been closed.