React Data GridRow Grouping - Sorting GroupsEnterprise
This section provides details on how to configure and customise how row groups are sorted.
When using Single Group Column or Multiple Group Columns, sorting can be enabled through the sortable
column property as shown below:
const defaultColDef = {
// enable sorting across all columns - including Row Group Columns
sortable: true,
};
const autoGroupColumnDef = {
// enabled sorting on Row Group Columns only
sortable: true,
};
// also applies to the 'multipleColumns' display type
const groupDisplayType = 'singleColumn';
<AgGridReact
defaultColDef={defaultColDef}
autoGroupColumnDef={autoGroupColumnDef}
groupDisplayType={groupDisplayType}
>
</AgGridReact>
It is common to simply define defaultColDef.sortable = true
across all columns, which includes row group columns.
However, in cases where it is preferable to define sorting on the Row Group Columns directly,
autoGroupColumnDef.sortable = true
can be used.
Sorting is also enabled using the sortable
column property with Custom Group Columns, as shown below:
const columnDefs = [
{
cellRenderer: 'agGroupCellRenderer',
showRowGroup: true,
// enabled sorting on this Row Group Column only
sortable: true,
},
];
const defaultColDef = {
// enable sorting across all columns - including Row Group Columns
sortable: true,
};
const groupDisplayType = 'custom';
<AgGridReact
columnDefs={columnDefs}
defaultColDef={defaultColDef}
groupDisplayType={groupDisplayType}
>
</AgGridReact>
The example below demonstrates how sorting is enabled with Multiple Group Columns.
Note that sorting is enabled across all columns, including Row Group Columns, using: defaultColDef.sortable = true
.
By default, columns only apply sorting to their leaf level row data, as such to sort row groups you should apply the sort to the column on which the grouping has been applied.
This creates the side effect that groups can be sorted in multiple directions simultaneously, the group column reflects this by displaying the multi-sort icon when it does not match one or more of the provided columns sort direction.
const columnDefs = [
{
field: 'year',
rowGroup: true,
sortable: true,
sort: 'desc',
},
{
field: 'handset',
rowGroup: true,
sortable: true,
sort: 'asc',
},
];
<AgGridReact columnDefs={columnDefs}></AgGridReact>
In this snippet, sorting on the year or handset column will sort their respective row groups. As the year and handset column have different sorts applied to them, the group column displays the mixed sort icon.
The following example demonstrates mixed group sorting. Note the following:
- Click the header of the group column to apply a sort, observe how it forces the year and handset columns sort to match.
- Hold shift and click the header of the year column, observe how the sort direction is now different from the auto column, and the auto column now displays the mixed-sort icon.
By default, any sort comparator
defined on a column that is used to group rows by will also be used by the Group Column.
For example, consider the following column definition:
const columnDefs = [
{
field: 'month',
rowGroup: true,
comparator: (a, b) => {
const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July',
'August', 'September','October', 'November', 'December'];
// sorts 'months' in chronological order
return months.indexOf(a) - months.indexOf(b);
},
},
];
<AgGridReact columnDefs={columnDefs}></AgGridReact>
As rowGroup = true
is defined on this column, the supplied comparator
will be used to sort the month
column and
the Group Column.
The following example demonstrates custom group sorting. Note the following:
- The
month
column has a custom sortcomparator
supplied which sorts months in chronological order. - The 'Group' Column uses the
comparator
defined on themonth
column definition to sort the row groups.
It is also possible to define a comparator that will be used across all group levels using; autoGroupColumnDef.comparator
.
This 'shared group comparator' will override over any comparators defined on the underlying columns.
When sorting on non-group columns it may be desirable to maintain the existing group order. This behaviour can be
enabled through the groupMaintainOrder
grid option as shown below:
const columnDefs = [
{ field: 'assignee', rowGroup: true, hide: true },
{ field: 'priority', rowGroup: true, hide: true },
{ field: 'task' },
];
const groupDisplayType = 'multipleColumns';
// preserves current group order when sorting on non-group columns
const groupMaintainOrder = true;
<AgGridReact
columnDefs={columnDefs}
groupDisplayType={groupDisplayType}
groupMaintainOrder={groupMaintainOrder}
>
</AgGridReact>
Note that Group Order is not the same as sorting. Maintaining group order will not preserve active group sorts, just the current order of the groups. However, when sorting on group columns the group order will be changed based on the sort.
The following example demonstrates how groupMaintainOrder
works. Note the following:
groupMaintainOrder = true
is defined on the grid options supplied to the grid.- Clicking on the 'Task' column header only sorts the tasks without reordering the groups.
- Sorting on the 'Assignee' or 'Priority' group column headers will reorder the groups.
Continue to the next section to learn about Filtering Groups.