# Grouping Basics

The KendoReact Grid lets you display grouped table data.

<CtaPanelOverview></CtaPanelOverview>

## Enabling Grouping

The KendoReact Grid supports grouping in two modes:

* [Built-in State Management](https://www.telerik.com/kendo-react-ui/components/grid/grouping#using-the-built-in-state-management-for-grouping): The Grid manages its own grouping state internally.

* [Controlled Mode](https://www.telerik.com/kendo-react-ui/components/grid/grouping#using-the-grouping-in-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:

1. Enable the [`autoProcessData`](https://www.telerik.com/kendo-react-ui/components/grid/api/gridprops#autoprocessdata) prop to let the Grid handle grouping automatically.

2. Set the [`groupable`](https://www.telerik.com/kendo-react-ui/components/grid/api/gridprops#groupable) prop of the Grid to enable grouping features.

3. Set the `dataItemKey` prop to a unique value field from the data in the Grid.

4. (Optional) Set the [`defaultGroup`](https://www.telerik.com/kendo-react-ui/components/grid/api/gridprops#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.

[Demo Source](https://demos.telerik.com/kendo-react-ui/grid/grouping/built-in/func/source.txt)

### Using the Grouping in Controlled Mode

To use grouping in the KendoReact Grid with controlled mode, follow these steps:

1. Set the [`groupable`](https://www.telerik.com/kendo-react-ui/components/grid/api/gridprops#groupable) and [`group`](https://www.telerik.com/kendo-react-ui/components/grid/api/gridprops#group) options of the Grid.
2. Handle the [`onGroupChange`](https://www.telerik.com/kendo-react-ui/components/grid/api/gridprops#ongroupchange) or the [`onDataStateChange`](https://www.telerik.com/kendo-react-ui/components/grid/api/gridprops#ondatastatechange) event. The `onDataStateChange` event works best when the Grid has other data operations because it provides the complete [`dataState`](https://www.telerik.com/kendo-react-ui/components/dataquery/api/state) in a single event.
3. Group the data on the client by using the [`groupBy`](https://www.telerik.com/kendo-react-ui/components/dataquery/api/groupby) or [`process`](https://www.telerik.com/kendo-react-ui/components/dataquery/api/process) 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 of [`GroupResults`](https://www.telerik.com/kendo-react-ui/components/dataquery/api/groupresult).

> Use the [`groupBy`](https://www.telerik.com/kendo-react-ui/components/dataquery/api/groupby) method with the `onGroupChange` event, and the [`process`](https://www.telerik.com/kendo-react-ui/components/dataquery/api/process) method with the `onDataStateChange` event.

For more information, see the article on the [process helpers for bulk data operations](https://www.telerik.com/kendo-react-ui/components/dataquery).

The following example demonstrates grouping in controlled mode, where the `group` state is managed externally and passed to the Grid alongside an `onGroupChange` handler.

[Demo Source](https://demos.telerik.com/kendo-react-ui/grid/grouping/controlled/func/source.txt)

## 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`](https://www.telerik.com/kendo-react-ui/components/grid/api/gridcolumnprops#groupable) property.

```jsx
<Column field="ProductID" filterable={false} title="ID" width="50px" groupable={isGroupable("ProductID")} />
```

```jsx
const [group, setGroup] = React.useState<GroupDescriptor[]>(initialGroup);

const isGroupable = (field) => {
    // Disable grouping for fields that are already grouped.
    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.

The following example demonstrates how to persist the collapsed state of group headers across data reloads using unique group item IDs from the `data-tools` package.

[Demo Source](https://demos.telerik.com/kendo-react-ui/grid/grouping-persist/func/source.txt)

## Expand and Collapse All Groups

The example below shows how to add a button that expands or collapses all groups in the Grid.

[Demo Source](https://demos.telerik.com/kendo-react-ui/grid/grouping-expand-collapse/func/source.txt)

## Suggested Links

* [GroupDescriptor](https://www.telerik.com/kendo-react-ui/components/dataquery/api/groupdescriptor)
* [dataStateChange](https://www.telerik.com/kendo-react-ui/components/grid/api/gridprops#ondatastatechange)
* [API Reference of the Grid](https://www.telerik.com/kendo-react-ui/components/grid/api/gridprops)
