Row Grouping - Use multiple columns in group header

Row Grouping - Use multiple columns in group header

slamduncslamdunc Posts: 1Questions: 1Answers: 0
edited July 2016 in Free community support

Hi all,

I have a datatable with and output that looks like this -

Job Description 1 | Job Classification 1 | Rate A | Pay Rate | Charge Rate
Job Description 1 | Job Classification 1 | Rate B | Pay Rate | Charge Rate
Job Description 1 | Job Classification 1 | Rate C | Pay Rate | Charge Rate
Job Description 2 | Job Classification 2 | Rate A | Pay Rate | Charge Rate
Job Description 2 | Job Classification 2 | Rate B | Pay Rate | Charge Rate
Job Description 2 | Job Classification 2 | Rate C | Pay Rate | Charge Rate

I'd like to use grouping to so that it displays like this -

Job Description 1 - Job Classification 1


Rate A | Pay Rate | Charge Rate
Rate B | Pay Rate | Charge Rate
Rate C | Pay Rate | Charge Rate

Job Description 2 - Job Classification 2


Rate A | Pay Rate | Charge Rate
Rate B | Pay Rate | Charge Rate
Rate C | Pay Rate | Charge Rate

I can get them on to 2 header rows using using the drawCallback function but can't figure out how to get these both in the same row -

Job Description 2
Job Classification 2


Rate A | Pay Rate | Charge Rate
Rate B | Pay Rate | Charge Rate
Rate C | Pay Rate | Charge Rate

   var api = this.api();
        var rows = api.rows({ page: 'current' }).nodes();
        var last = null;
        var columns = [0, 1];

        for (c = 0; c < columns.length; c++) {
            var colNo = columns[c];
            api.column(c, { page: 'current' }).data().each(function (group, i) {
                if (last !== group) {
                    $(rows).eq(i).before(
                        '<tr class="group"><td colspan="5">' + group + '</td></tr>'
                    );

                    last = group;
                }
            });
        }
    }

I've seen a couple of people asking before but there is no solution provided. Is this possible?

I'd also like to include a checkbox in the header with an ID but I'm sure that'll be easy enough to figure out when I know how to use multiple columns in the header.

Answers

  • amish.patelamish.patel Posts: 1Questions: 0Answers: 0

    You can use following code and it will allow you to group by second column.
    Here rowData[1] , is the value from your second column (indexed at 1).

                var api = this.api();
                var rows = api.rows( {page:'current'} ).nodes();
                var last=null;
    
                api.column(0, {page:'current'} ).data().each( function ( group, i ) {
    
                     if (last !== group) {
                         var rowData = api.row(i).data(); 
    
                         $(rows).eq(i).before(
                         '<tr class="group"><td colspan="5">' + group + " - " + rowData[1] + '</td></tr>'
                     );
                         last = group;
                     }
                });
    
This discussion has been closed.