Grouping BasicsPremium
The KendoReact Data Grid lets you display grouped table data.
Enabling Grouping
The KendoReact Grid supports grouping in two modes:
-
Built-in State Management: The Grid manages its own grouping state internally.
-
Controlled Mode: You manage the grouping state externally by handling events and updating the state.
Using the Built-in State Management for Grouping
To use grouping with the built-in state management, follow these steps:
-
Enable the
autoProcessData
prop to let the Grid handle grouping automatically. -
Set the
groupable
prop of the Grid to enable grouping features. -
Set the
dataItemKey
prop to a unique value field from the data in the Grid. -
(Optional) Set the
defaultGroup
prop to add initial grouping for the Grid.
The following example shows how to use grouping with the built-in state management of the KendoReact Grid.
Using the Grouping in Controlled Mode
To use grouping in the KendoReact Grid with controlled mode, follow these steps:
- Set the
groupable
andgroup
options of the Grid. - Handle the
onGroupChange
or theonDataStateChange
event. TheonDataStateChange
event works best when the Grid has other data operations because it provides the completedataState
in a single event. - Group the data on the client by using the
groupBy
orprocess
methods. You can also group data on the server by making a request using the event parameters. The Grid needs the grouped data as a collection ofGroupResults
.
Use the
groupBy
method with theonGroupChange
event, and theprocess
method with theonDataStateChange
event.
For more information, see the article on the process helpers for bulk data operations.
Grouping Columns Dynamically
By default, you can group all columns of the Grid multiple times. To enable grouping of specific Grid columns and add dynamic grouping to a column, use a function or a variable for the groupable
property.
<Column field="ProductID" filterable={false} title="ID" width="50px" groupable={isGroupable("ProductID")} />
const [group, setGroup] = React.useState<GroupDescriptor[]>(initialGroup);
const isGroupable = (field) => {
return !((group || []).find((g) => g.field === field));
}
Persist Groups Collapsed State
The data-tools
package gives you utility methods to create unique group item IDs. You can use these IDs to save the group collapsed state.
Expand and Collapse All Groups
The example below shows how to add a button that expands or collapses all groups in the Grid.