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.
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 groups and in any order. What you need to understand when defining as follows:
A group can have children initially hidden. If you want to show or hide children, set columnGroupShow to either 'open' or 'closed' to one or more of the children. When a children set has columnGroupShow set, it behaves in the following way:
If a group has any child that is dependent on the open / closed state, the open / close icon will appear. 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' columns 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 (ie 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 color 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.
Here is a basic example of grouping in action.
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:
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: