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 gridOptions = {
columnDefs: [
{
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' },
]
}
],
// other grid options ...
}Below shows an example of column group configuration.
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:
children attribute is mandatory for Column Groups and not applicable for Columns.children attribute, it is treated as a Column Group. If it does not have a children attribute, it is treated as a Column.groupId is only used for groups). If you provide attributes that are not applicable (eg you give a column a groupId) they will be ignored.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').
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).
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.
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.

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.
columnDefs: [
// the CSS class name supplied to 'headerClass' will get applied to the header group
{ headerName: 'Athlete Details', headerClass: 'my-css-class', children: []}
],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-alpine .ag-header-group-cell-label {
flex-direction: row-reverse;
}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:
marryChildren=true.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:
suppressStickyLabel=true.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:
colDef.openByDefault property, where it sets this on E and F groups, resulting in these groups appearing as open by default.defaultColGroupDef and defaultColDef to apply a class to some of the headers. Using this technique, you can apply style to any of the header sections.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:
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:
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:
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 gridOptions = {
columnDefs: [
{
headerName: 'Athlete Details',
children: [
{ field: 'athlete' },
{ field: 'country' },
],
},
{
field: 'age',
width: 90,
suppressSpanHeaderHeight: true
}
],
// other grid options ...
}Note the following: