# Angular TreeList Expanded State

The TreeList enables you to define and persist the expanded state of its items.

## Getting Started

The TreeList exposes the following options to control the expand state:

* [`isExpanded`](https://www.telerik.com/kendo-angular-ui-develop/components/treelist/api/treelistcomponent#isexpanded) function—Determines if a given item is expanded.
* [`collapse`](https://www.telerik.com/kendo-angular-ui-develop/components/treelist/api/treelistcomponent#collapse) event—Fired when a given item is about to be collapsed.
* [`expand`](https://www.telerik.com/kendo-angular-ui-develop/components/treelist/api/treelistcomponent#expand) event—Fired when a given item is about to be expanded.

> If `isExpanded` is not set, the TreeList will expand all items and will not show expand / collapse icons for the expandable column.

The following example demonstrates how to control the expanded state of the TreeList items.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/treelist/expanded-state/basic/source.txt)

## Built-in Directive

The TreeList provides a built-in [`ExpandableDirective`](https://www.telerik.com/kendo-angular-ui-develop/components/treelist/api/expandabledirective) to simplify controlling the expanded state. The `kendoTreeListExpandable` directive automatically handles the `expand` and `collapse` events and provides the `isExpanded` function to the TreeList.

```html
<kendo-treelist
    [kendoTreeListFlatBinding]="data"
    kendoTreeListExpandable
    ...
>
    ...
</kendo-treelist>
```

The following example demonstrates how to use the built-in directive.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/treelist/expanded-state/directive/source.txt)

### Setting the Expanded Items

To set the initially expanded items, bind an array with the expanded item keys to the [`expandedKeys`](https://www.telerik.com/kendo-angular-ui-develop/components/treelist/api/expandabledirective#expandedkeys) input.
By default, the directive uses the TreeList [`idField`](https://www.telerik.com/kendo-angular-ui-develop/components/treelist/api/treelistcomponent#idfield) as key. If the `idField` is not set, the directive will use the item reference.

```html
<kendo-treelist
    [kendoTreeListFlatBinding]="data"
    kendoTreeListExpandable
    [(expandedKeys)]="expandedIds"
    idField="id"
    ...
>
    ...
</kendo-treelist>
```

The following example demonstrates how to set the initially expanded items using the id field.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/treelist/expanded-state/persist-state/source.txt)

### Custom Expand Field

To change the field which is used to persist the expanded items, set the [`expandBy`](https://www.telerik.com/kendo-angular-ui-develop/components/treelist/api/expandabledirective#expandby) input to the field name or a function that takes the item and returns the key value.

```html
<kendo-treelist
    [kendoTreeListFlatBinding]="data"
    kendoTreeListExpandable
    expandBy="name"
    ...
>
    ...
</kendo-treelist>
```

The following example demonstrates how to persist the expanded using a custom field.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/treelist/expanded-state/expand-by/source.txt)

### Initially Expanded

The directive supports expanding all items by default via the [`initiallyExpanded`](https://www.telerik.com/kendo-angular-ui-develop/components/treelist/api/expandabledirective#initiallyexpanded) option.

> If `initiallyExpanded` is set to true, `expandedKeys` will hold the keys of the collapsed items.

The following example demonstrates how to expand all items on initialization.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/treelist/expanded-state/initially-expanded/source.txt)

## Suggested Links

* [API Reference of the TreeListComponent](https://www.telerik.com/kendo-angular-ui-develop/components/treelist/api/treelistcomponent)
* [API Index of the TreeList](https://www.telerik.com/kendo-angular-ui-develop/components/treelist/api)
