It can often be useful to use grouping rows both at the start and the end of a group, each providing different summary information about the group. RowGroup
provides two rendering options - rowGroup.startRender
and rowGroup.endRender
- as the naming implies, one for the group start and one for the group end, allowing them to be
configured independently.
This example shows the rowGroup.startRender
left with its default state, while the rowGroup.endRender
shows a summary
of the information in the group - in this case an average salary.
Name | Position | Office | Age | Start date | Salary |
---|---|---|---|---|---|
Name | Position | Office | Age | Start date | Salary |
Edinburgh | |||||
Tiger Nixon | System Architect | Edinburgh | 61 | 2011/04/25 | $320,800 |
Cedric Kelly | Senior Javascript Developer | Edinburgh | 22 | 2012/03/29 | $433,060 |
Sonya Frost | Software Engineer | Edinburgh | 23 | 2008/12/13 | $103,600 |
Quinn Flynn | Support Lead | Edinburgh | 22 | 2013/03/03 | $342,000 |
Dai Rios | Personnel Lead | Edinburgh | 35 | 2012/09/26 | $217,500 |
Gavin Joyce | Developer | Edinburgh | 42 | 2010/12/22 | $92,575 |
Martena Mccray | Post-Sales support | Edinburgh | 46 | 2011/03/09 | $324,050 |
Jennifer Acosta | Junior Javascript Developer | Edinburgh | 43 | 2013/02/01 | $75,650 |
Shad Decker | Regional Director | Edinburgh | 51 | 2008/11/13 | $183,000 |
Average salary in Edinburgh: $232,471 | |||||
London | |||||
Jena Gaines | Office Manager | London | 30 | 2008/12/19 | $90,560 |
Average salary in London: $90,560 |
The Javascript shown below is used to initialise the table shown in this example:
$(document).ready(function() {
$('#example').DataTable( {
order: [[2, 'asc']],
rowGroup: {
endRender: function ( rows, group ) {
var avg = rows
.data()
.pluck(5)
.reduce( function (a, b) {
return a + b.replace(/[^\d]/g, '')*1;
}, 0) / rows.count();
return 'Average salary in '+group+': '+
$.fn.dataTable.render.number(',', '.', 0, '$').display( avg );
},
dataSrc: 2
}
} );
} );
In addition to the above code, the following Javascript library files are loaded for use in this example:
The HTML shown below is the raw HTML table element, before it has been enhanced by DataTables:
This example uses a little bit of additional CSS beyond what is loaded from the library files (below), in order to correctly display the table. The additional CSS used is shown below:
table.dataTable tr.group-end td {
text-align: right;
font-weight: normal;
}
The following CSS library files are loaded for use in this example to provide the styling of the table:
This table loads data by Ajax. The latest data that has been loaded is shown below. This data will update automatically as any additional data is loaded.
The script used to perform the server-side processing for this table is shown below. Please note that this is just an example script using PHP. Server-side processing scripts can be written in any language, using the protocol described in the DataTables documentation.