Vyache
Vyache

Reputation: 382

Datatables: Using Row grouping, how do I get a count of each grouped row?

I'm using Datatables to create a grouped by data grid. I was able to group my table using my first column.

I want to accomplish getting the count on each grouped row and writing out the count on my grouped row, how would I go about doing that?

If you see the link below, they have the shaded grouped row. I would like the count to appear right after the text of the grouped text.

http://www.datatables.net/examples/advanced_init/row_grouping.html

Thank you.

Upvotes: 6

Views: 7255

Answers (5)

Tremarctos
Tremarctos

Reputation: 21

try with this code...

  rowGroup: {
        endRender: function ( rows, group ) {
            var avg = rows
              .rows()
              .data()
              .filter( function ( data, index ) {
                return data[ 'group_name' ] == group ? true : false;
              } )
              .count();

            return '<div width="100%" align="left" style="font-size : 14px; background-color: orangered; color: white; padding-right: 15px;"><strong>Rows of  '+group+': '+
                $.fn.dataTable.render.number(',', '.', 0, '').display( avg ) + '</strong></div>';
        },
        dataSrc: "group_name"
    }, 

Upvotes: 1

Praveenkumar M G
Praveenkumar M G

Reputation: 47

This can be accomplished by adding a for loop inside drawCallBack function. Referring to http://www.datatables.net/examples/advanced_init/row_grouping.html for drawCallBack function. Please note I am using legacy dataTable function i.e fnDrawCallBack replace that with latest equivalent.

            fnDrawCallback: function(settings) {
            var api = this.api();
            var rows = api.rows({ page: "current" }).nodes();
            var last = null;
            var storedIndexArray = [];
            api.column(0, { page: "current" })
                .data()
                .each((group, i) => {
                    if (last !== group) {
                        storedIndexArray.push(i);
                        $(rows)
                            .eq(i)
                            .before(
                                '<tr class="group"><td colspan="4">' +
                                    group +
                                    "<span class='group-count'></span></td></tr>"
                            );
                        last = group;
                    }
                });
            storedIndexArray.push(
                api.column(0, { page: "current" }).data().length
            );
            for (let i = 0; i < storedIndexArray.length - 1; i++) {
                let element = $(".group-count")[i];
                $(element).text(
                    storedIndexArray[i + 1] - storedIndexArray[i]
                );
            }
        }

Upvotes: 4

Carl Chilton
Carl Chilton

Reputation: 21

you can do this via the rowGroup property:

    rowGroup: {dataSrc: 'Your column name or index',
        startRender: function (rows, group) {
            return group + ' (' + rows.count() + ' rows)';
        }
    }

Upvotes: 2

user10767645
user10767645

Reputation: 21

function CountRows(){
    debugger;
    var abc = $('.abc');
    var arr = [];

    $( abc ).each(function( index, value ) {
        var dynamicId = (this.id.split('_')[1]); 
        var groupRowCount = $('.group_'+dynamicId).length;
        this.textContent = this.textContent + " (" + groupRowCount + ")"
    });

}  

Upvotes: 2

bpeterson76
bpeterson76

Reputation: 12870

Using the example you linked, noted my comments with //:

"drawCallback": function ( settings ) {
        var api = this.api();
        var rows = api.rows( {page:'current'} ).nodes();
        var last=null;

        api.column(2, {page:'current'} ).data().each( function ( group, i ) {

        //Create vars with each element you want to sum here

            if ( last !== group ) {
                $(rows).eq( i ).before(
                    //Get row data here, sum it in vars to display
                    '<tr class="sum group"><td>'+SumCol1+'</td><td>'+SumCol2+'</td>
                    //End sum row
                    '<tr class="group"><td colspan="5">'+group+'</td></tr>'
                );


                last = group;
                //Don't forget to clear your sum vars at the end of each loop!
            }
        } );
    }

Upvotes: -2

Related Questions