# Grouping Basics

The Kendo UI DataGrid enables you to display grouped table data.

## Enabling Grouping

The Kendo UI for Vue Grid supports grouping in two modes:

* [Built-in State Management](https://www.telerik.com/kendo-vue-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-vue-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-vue-ui/components/grid/api/gridprops#autoprocessdata) prop to let the Grid handle grouping automatically.

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

3. Set the [`dataItemKey`](https://www.telerik.com/kendo-vue-ui/components/grid/api/gridprops#dataitemkey) prop to a unique value field from the data in the Grid.

4. (Optional) Set the [`defaultGroup`](https://www.telerik.com/kendo-vue-ui/components/grid/api/gridprops#defaultGroup) prop to add initial grouping for the Grid.

```vue
<Grid :auto-process-data="true" :groupable="true" />
```

The following example shows how to use grouping with the built-in state management of the Kendo UI for Vue Grid.

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

### Using the Grouping in Controlled Mode

To use grouping in the Kendo UI for Vue Grid with controlled mode, follow these steps:

1. Set the [`groupable`](https://www.telerik.com/kendo-vue-ui/components/grid/api/gridprops#groupable) and [`group`](https://www.telerik.com/kendo-vue-ui/components/grid/api/gridprops#group) props of the Grid.
2. Handle the [`groupchange`](https://www.telerik.com/kendo-vue-ui/components/grid/api/gridprops#ongroupchange) or [`datastatechange`](https://www.telerik.com/kendo-vue-ui/components/grid/api/gridprops#ondatastatechange) event. Use `datastatechange` when the Grid has other data operations because it provides the complete [`State`](https://www.telerik.com/kendo-vue-ui/components/dataquery/api/state) object in one event.
3. Group data on the client with [`groupBy`](https://www.telerik.com/kendo-vue-ui/components/dataquery/api/groupby) or [`process`](https://www.telerik.com/kendo-vue-ui/components/dataquery/api/process). You can also group data on the server by using the event parameters. Provide the Grid with a collection of [`GroupResult`](https://www.telerik.com/kendo-vue-ui/components/dataquery/api/groupresult) items.

> Use [`groupBy`](https://www.telerik.com/kendo-vue-ui/components/dataquery/api/groupby) with `groupchange`, and use [`process`](https://www.telerik.com/kendo-vue-ui/components/dataquery/api/process) with `datastatechange`.

For more information, see the [Data Query process helpers](https://www.telerik.com/kendo-vue-ui/components/dataquery) documentation.

The following example shows how to use grouping in controlled mode where you manage the grouping state by handling events.

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

## Grouping Columns Dynamically

By default, you can group all Grid columns multiple times. To enable grouping for specific columns and control it dynamically, use a function or variable for the [`groupable`](https://www.telerik.com/kendo-vue-ui/components/grid/api/gridcolumnprops#groupable) property.

```vue
<Grid :columns="[
  { field: 'ProductID', filterable: false, title: 'ID', width: '50px', groupable: isGroupable('ProductID') }
]" />
```

```vue
<script setup lang="ts">
import { ref, computed } from 'vue';
import type { GroupDescriptor } from '@progress/kendo-data-query';

const group = ref<GroupDescriptor[]>(initialGroup);

const isGroupable = (field: string) => {
  return !((group.value || []).find((groupDescriptor) => groupDescriptor.field === field));
};
</script>
```

## Persist Groups Collapsed State

The Data Tools [`setGroupIds`](https://www.telerik.com/kendo-vue-ui/components/datatools/api/setgroupids) utility creates unique group item IDs. Use these IDs with the [`groupExpand`](https://www.telerik.com/kendo-vue-ui/components/grid/api/gridprops#groupexpand) state to preserve the expanded or collapsed state of grouped items.

The following example demonstrates how to preserve the expanded and collapsed state of grouped items while the processed data changes. It uses `setGroupIds` from Data Tools, `groupBy` from Data Query, and a ref for the `groupExpand` state.

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

## Expand and Collapse All Groups

Store group expansion state in the `groupExpand` prop. To collapse all groups, set `expanded` to `false` in the descriptors for each group. To restore the default expanded state, clear the `groupExpand` descriptors.

The following example demonstrates how to expand and collapse all group items by clicking a toggle button. It updates the `groupExpand` descriptors for every visible group.

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

## Suggested Links

* [`datastatechange` Event](https://www.telerik.com/kendo-vue-ui/components/grid/api/gridprops#ondatastatechange)
* [Grid API Reference](https://www.telerik.com/kendo-vue-ui/components/grid/api/gridprops)
* [Grid Custom Cell with Grouping and Selection](https://www.telerik.com/kendo-vue-ui/components/knowledge-base/grid-custom-cell-grouping-and-selection)
