Angular TreeList - Expand and Collapse Rows
TreeList allows you to configure expanded rows on initialization, and expand/collapse rows programmatically.
API
All TreeList rows are initially collapsed. If you enable autoExpandAll, all rows are initially expanded. Specify the expandedRowKeys array to expand specific rows. To expand a nested row, include all parent row keys (in addition to the nested row key) in expandedRowKeys.
- <!-- Expand specific rows -->
- <dx-tree-list ...
- [expandedRowKeys]="[1, 5, 18]"
- >
- <!-- ... -->
- </dx-tree-list>
- <!-- Expand all rows -->
- <dx-tree-list ...
- [autoExpandAll]="true"
- >
- <!-- ... -->
- </dx-tree-list>
- import { DxTreeListModule } from "devextreme-angular";
- // ...
- @NgModule({
- imports: [
- // ...
- DxTreeListModule
- ],
- })
To expand/collapse rows at runtime, call expandRow(key)/collapseRow(key) or modify the expandedRowKeys array. To collapse all rows, set expandedRowKeys to an empty array ([]).
To toggle a row between expanded and collapsed states, you can implement the isRowExpanded(key) method as follows:
- import { ViewChild } from "@angular/core";
- import { DxTreeListComponent } from "devextreme-angular/ui/tree-list";
- // ...
- export class AppComponent {
- @ViewChild(DxTreeListComponent, { static: false }) treeList!: DxTreeListComponent;
- toggleRow(key) {
- if (this.treeList.instance.isRowExpanded(key)) {
- this.treeList.instance.collapseRow(key);
- } else {
- this.treeList.instance.expandRow(key);
- }
- }
- }
See Also
Events
To execute certain commands before or after a row was expanded or collapsed, handle the rowExpanding, rowExpanded, rowCollapsing or rowCollapsed event. Assign event handling functions to the corresponding onEventName properties when you configure the UI component if these functions are going to remain unchanged.
- <dx-tree-list ...
- (onRowExpanding)="onRowExpanding($event)"
- (onRowExpanded)="onRowExpanded($event)"
- (onRowCollapsing)="onRowCollapsing($event)"
- (onRowCollapsed)="onRowCollapsed($event)">
- </dx-tree-list>
- import { DxTreeListModule } from "devextreme-angular";
- // ...
- export class AppComponent {
- onRowExpanding (e) {
- // Handler of the "rowExpanding" event
- },
- onRowExpanded (e) {
- // Handler of the "rowExpanded" event
- },
- onRowCollapsing (e) {
- // Handler of the "rowCollapsing" event
- },
- onRowCollapsed (e) {
- // Handler of the "rowCollapsed" event
- }
- }
- @NgModule({
- imports: [
- // ...
- DxTreeListModule
- ],
- // ...
- })