# Controlling the Expanded State

The [Angular Grid](https://www.telerik.com/kendo-angular-ui-develop/components/grid) enables you to define and persist the expanded state of its groups and their hierarchical structure.

## Built-in Directive

The Grid provides the built-in [`kendoGridExpandGroupBy`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/expandgroupdirective) directive to simplify controlling the expanded state of group rows.

### Default Configuration

The [`kendoGridExpandGroupBy`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/expandgroupdirective) directive automatically handles the [`groupCollapse`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/gridcomponent#groupcollapse) and [`groupExpand`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/gridcomponent#groupexpand) events and provides the default [`isGroupExpanded`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/gridcomponent#isgroupexpanded) function to the Grid.

By default, the directive will store a collection of [`GroupKey`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/groupkey) items for each expanded group. Each [`GroupKey`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/groupkey) holds the following properties:

* `field`—The `field` property value of the current group item.
* `value`—The `value` property value of the current group item.
* `parentGroupKeys`—An array of `{ field: string, value: any }` items indicating all parent groups of the current one, staring from the direct parent group all the way to the root level group.

> As the [`expandedGroupKeys`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/expandgroupdirective#expandedgroupkeys) collection includes the paths to the parent groups, when the [`groupChange`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/gridcomponent#groupchange) event is fired, some of the group keys may no longer be valid, as the paths to the parent groups may have changed. To avoid any unexpected behavior, the [`expandedGroupKeys`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/expandgroupdirective#expandedgroupkeys) collection must either be cleared or each item's parent group keys updated.

The following example demonstrates using the directive.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/grid/grouping/persisting-state/expanded-state-directive/default-expandby/source.txt)

### Custom Group Key Format

To override the default key persistence format, provide to the [`kendoGridExpandGroupBy`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/expandgroupdirective) directive selector a callback that accepts a [`GroupRowArgs`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/grouprowargs) object as a single argument and returns a unique primitive value key.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/grid/grouping/persisting-state/expanded-state-directive/custom-expandby/source.txt)

### Expand and Collapse All Groups

Use the [`kendoGridExpandGroupBy`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/expandgroupdirective) directive and its [`groupsInitiallyExpanded`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/expandgroupdirective#groupsinitiallyexpanded) option to expand or collapse all group rows in one go. To do so, reset the [`expandedGroupKeys`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/expandgroupdirective#expandedgroupkeys) collection and set [`groupsInitiallyExpanded`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/expandgroupdirective#groupsinitiallyexpanded) to the respective value.

The following example demonstrates this approach:

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

## Providing a Callback

The Grid exposes the [`isGroupExpanded`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/gridcomponent#isgroupexpanded) function to control the expanded state of each group row.

> The `isGroupExpanded` function takes precedence both over the default grouping behavior and over any possibly configured group option.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/grid/grouping/persisting-state/provide-callback/source.txt)

### Expand and Collapse All Groups via Callback

The [`isGroupExpanded`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/gridcomponent#isgroupexpanded) callback allows you to implement custom logic for expanding or collapsing all groups in one go when the [`kendoGridExpandGroupBy`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/expandgroupdirective) directive is not applied. To do so, reset the collection of expanded groups, and toggle a boolean flag indicating all groups are expanded or collapsed. The [`isGroupExpanded`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/gridcomponent#isgroupexpanded) callback can then use this information to return the respective value when all groups need to be expanded/collapsed.

The following example demonstrates this approach:

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

## Suggested Links

* [ExpandGroupDirective](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/expandgroupdirective)
* [API Reference of the GridComponent](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/gridcomponent)
* [API Index of the Grid](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api)
