React Data GridColumn Groups
Grouping columns allows you to have multiple levels of columns in your header and the ability, if you want, to 'open and close' column groups to show and hide additional columns.
Grouping columns is done by providing the columns in a tree hierarchy to the grid. There is no limit to the number of levels you can provide.
Here is a code snippet of providing two groups of columns.
const [columnDefs, setColumnDefs] = useState([
{
headerName: 'Athlete Details',
children: [
{ field: 'athlete' },
{ field: 'age' },
{ field: 'country' },
]
},
{
headerName: 'Sports Results',
children: [
{ field: 'sport' },
{ field: 'total', columnGroupShow: 'closed' },
{ field: 'gold', columnGroupShow: 'open' },
{ field: 'silver', columnGroupShow: 'open' },
{ field: 'bronze', columnGroupShow: 'open' },
]
}
]);
<AgGridReact columnDefs={columnDefs} />
Below shows an example of column group configuration.
Column Definitions vs Column Group Definitions
The list of Columns in gridOptions.columnDefs
can be a mix of Columns and Column Groups.
You can mix and match at will, every level can have any number of Columns and Column Groups and in any order.
The difference in Column vs Column Group definitions is as follows:
- The
children
attribute is mandatory for Column Groups and not applicable for Columns. - If a definition has a
children
attribute, it is treated as a Column Group. If it does not have achildren
attribute, it is treated as a Column. - Most other attributes are not common across groups and columns (eg
groupId
is only used for groups). If you provide attributes that are not applicable (eg you give a column agroupId
) they will be ignored.
Showing / Hiding Columns
A group can have children shown or hidden based on the open / closed state of the group. This is controlled by setting columnGroupShow
on one or more of the children. When a child has columnGroupShow
set, it behaves in the following way:
open
: The child is only shown when the group is open.closed
: The child is only shown when the group is closed.null
,undefined
: The child is always shown.
If a group has any child with columnGroupShow
set as open
/ closed
, then the open / close icon will appear in the group header. Otherwise the icon will not be shown.
Having columns only show when closed is useful when you want to replace a column with others. For example, in the code snippet above (and the example below), the 'Total' column is replaced with other columns when the group is opened.
If a group has an 'incompatible' set of children, then the group opening / closing will not be activated. An incompatible set is one which will have no columns visible at some point (i.e. all are set to 'open' or 'closed').
Pinning and Groups
Pinned columns break groups. So if you have a group with 10 columns, 4 of which are inside the pinned area, two groups will be created, one with 4 (pinned) and one with 6 (not pinned).
Moving Columns and Groups
If you move columns so that columns in a group are no longer adjacent, then the group will again be broken and displayed as one or more groups in the grid.
Resizing Groups
If you grab the group resize bar, it resizes each child in the group evenly distributing the new additional width. If you grab the child resize bar, only that one column will be resized.

Colouring Groups
The grid doesn't colour the groups for you. However you can use the column definition headerClass
for this purpose. The headerClass
attribute is available on both columns and column groups.
const [columnDefs, setColumnDefs] = useState([
// the CSS class name supplied to 'headerClass' will get applied to the header group
{ headerName: 'Athlete Details', headerClass: 'my-css-class', children: []}
]);
<AgGridReact columnDefs={columnDefs} />
Align the Header Group Label To The Right
The labels in the grouping headers are positioned with display: flex
. To make the group headers right-aligned, add the following rule set in your application, after the grid's stylesheets. Change the theme class to the one you use.
.ag-theme-quartz .ag-header-group-cell-label {
flex-direction: row-reverse;
}
Marry Children
Sometimes you want columns of the group to always stick together. To achieve this, set the column group property marryChildren=true
. The example below demonstrates the following:
- Both 'Athlete Details' and 'Sports Results' have
marryChildren=true
. - If you move columns inside these groups, you will not be able to move the column out of the group. For example, if you drag 'Athlete', it is not possible to drag it out of the 'Athlete Details' group.
- If you move a non group column, e.g. Age, it will not be possible to place it in the middle of a group and hence impossible to break the group apart.
- It is possible to place a column between groups (e.g. you can place 'Age' between the 'Athlete Details' and 'Sports Results').
Suppressing Sticky Label
When Column Groups are too wide, the Header Label is always visible while scrolling the grid horizontally. To suppress this behaviour, set the column group property suppressStickyLabel=true
. The example below demonstrates the following:
- Both 'Athlete Details' and 'Sport Results' have
suppressStickyLabel=true
. - If you scroll the grid horizontally, the header label will not be visible until the column is completely out of view.
Advanced Grouping Example
And here, to hammer in the 'no limit to the number of levels or groups', we have a more complex example. The grid here doesn't make much sense, it's just using the same Olympic Winners data and going crazy with the column groups. The example also demonstrates the following features:
- Using the API to open and close groups. To do this, you will need to provide your groups with an ID during the definition, or look up the groups ID via the API (as an ID is generated if you don't provide one).
- Demonstrates
colDef.openByDefault
property, where it sets this on E and F groups, resulting in these groups appearing as open by default. - Uses
defaultColGroupDef
anddefaultColDef
to apply a class to some of the headers. Using this technique, you can apply style to any of the header sections.
Group Changes
Similar to adding and removing columns, you can also add and remove column groups. If the column definitions passed in have column groups, then the columns will be grouped to the new configuration.
The example below shows adding and removing groups to columns. Note the following:
- Select No Groups to show all columns without any grouping.
- Select Participant in Group to show all participant columns only in a group.
- Select Medals in Group to show all medal columns only in a group.
- Select Participant and Medals in Group to show participant and medal columns in groups.
- As groups are added and removed, note that the state of the individual columns is preserved. To observe this, try moving, resizing, sorting, filtering etc and then add and remove groups, all the changed state will be preserved.
The example above shows adding and removing groups. It is also possible to add and remove columns from groups. This is demonstrated in the example below. Note the following:
- The example has two groups: Athlete Details and Sports Results
- The example has two sets of columns, Normal Cols and Extra Cols.
- When you move from Normal Cols to Extra Cols, three new columns are added to the list. Two belong to the Athlete Details group, the other belongs to no group.
Suppress Span Header Height
By default the grid will resize the header cell to span the whole height of the header container, as shown in the example below.
Note the following:
- The Age column header cell is not under a column group cell, but spans the entire height of the header container.
Using the Column Property suppressSpanHeaderHeight
the Grid will balance the column headers with different number of levels with an empty column group header cell, as shown in the example below.
const [columnDefs, setColumnDefs] = useState([
{
headerName: 'Athlete Details',
children: [
{ field: 'athlete' },
{ field: 'country' },
],
},
{
field: 'age',
width: 90,
suppressSpanHeaderHeight: true,
}
]);
<AgGridReact columnDefs={columnDefs} />
Note the following:
- The Age column has an empty column group header cell above it (shown with red borders).