Angular TreeList - Getting Started
DevExtreme includes multiple data management components. You can use them to display tabular, hierarchical, multidimensional data, or present information as cards. For more information about these components and their features, see How to Choose a Data Management Component.
DevExtreme TreeList displays data in a multi-column tree view. The component supports local and remote data stores and allows users to shape data using sorting, filtering, and other operations.
Need to create printable documents simply? Try our .NET-based DevExpress Reports: they ship with an intuitive Visual Studio Report Designer, Web Report Designer for end-user ad-hoc reporting, and a rich set of report controls, including cross tabs and charts.
You can generate a variety of report types — from simple mail-merge, table, and vertical reports to master-detail (hierarchical) and cross-tab reports, print or export them to PDF, Excel, and other formats.
Develop using VS Code? Leverage the capabilities of a brand new VS Code Report Designer extension to create and edit reports/documents on any platform, be it Windows, macOS, or Linux.
Get Started with DevExpress Reports | Explore Demos
Use our DevExpress BI Dashboard to embed interactive business intelligence into your next web app.
The Web Dashboard is a data analysis UI component that you can embed into your ASP.NET Core or Angular, React, and Vue applications with .NET backend. Dashboards allow you to display multiple inter-connected data analysis elements such as grids, charts, maps, gauges, and others: all within an automatically-arranged layout.
The set of components allows you to deploy an all-in-one solution and switch between Viewer and Designer modes directly on the web client (includes adaptive layouts for tablet & mobile).
The Web Dashboard is available as a part of a Universal subscription.
This tutorial adds DevExtreme TreeList to a page, binds the component to data, and configures core TreeList features.
Each section in this tutorial covers a single configuration step. You can also find the full code in the following GitHub repository:
Create a TreeList
Add DevExtreme to your Angular application and use the following code to create a TreeList:
- <dx-tree-list id="tree-list"
- <!-- Configuration goes here -->
- >
- </dx-tree-list>
- import { Component } from '@angular/core';
- import { DxTreeListModule } from 'devextreme-angular/ui/tree-list';
- @Component({
- // ...
- standalone: true,
- imports: [DxTreeListModule]
- })
- export class AppComponent { }
Bind the TreeList to Data
TreeList supports plain and hierarchical data structures. To use a hierarchical data source, set dataStructure to "tree" and define itemsExpr to specify the data field that contains child nodes. TreeList automatically generates identifiers for all nodes and builds the node hierarchy. Refer to the following demo for more information:
TreeList - Simple Array: Hierarchical Structure Demo
In a plain data structure, nodes must include unique identifiers and references to parent nodes. To specify data fields that store identifiers and references, use the keyExpr and parentIdExpr properties. Top-level nodes descend from the root node. To specify the root identifier, define rootValue.
TreeList can load and update data from different data source types. To use a local array, assign the array to dataSource. The following code snippet initializes a TreeList and creates columns for all data fields. All columns are equal width, and the column order follows the data source structure:
- <dx-tree-list
- [dataSource]="employees()"
- [rootValue]="-1"
- keyExpr="ID"
- parentIdExpr="HeadID">
- </dx-tree-list>
- import { inject, signal } from '@angular/core';
- import { EmployeesService, type Employee } from './employees.service';
- // ...
- export class AppComponent {
- private readonly employeesService = inject(EmployeesService);
- employees = signal<Employee[]>(this.employeesService.getEmployees());
- }
- import { Injectable } from '@angular/core';
- export interface Employee {
- ID: number;
- HeadID: number;
- FullName: string;
- Position: string;
- City: string;
- State: string;
- Email: string;
- Skype: string;
- MobilePhone: string;
- BirthDate: string;
- HireDate: string;
- }
- @Injectable({
- providedIn: 'root'
- })
- export class EmployeesService {
- private employees: Employee[] = [
- // ...
- ];
- getEmployees(): Employee[] {
- return this.employees;
- }
- }
set() or update() to modify values. Refer to the following help topic for more information: Zoneless Change Detection - Callback Functions.To use another data source type, refer to the following help topics:
Expand Rows
Read Tutorial: TreeList - Expand and Collapse Rows
You can define expandedRowKeys to expand specific rows. Enable autoExpandAll to initialize all rows in the expanded state:
- <dx-tree-list
- [autoExpandAll]="true"
- >
- <!-- ... -->
- </dx-tree-list>
Customize Columns
Read Tutorial: Columns - Overview
Specify the columns array to define and customize TreeList columns. Specify each column's dataField value.
Reorder Columns
Read Tutorial: Column Reordering
To set the initial column order, arrange columns in the columns[] array as needed. Enable allowColumnReordering to allow users to reorder columns in the component UI.
The following code snippet also specifies dataType for BirthDate and HireDate columns to display string values as dates:
- <dx-tree-list [allowColumnReordering]="true">
- <dxi-tree-list-column dataField="FullName"></dxi-tree-list-column>
- <dxi-tree-list-column dataField="Position"></dxi-tree-list-column>
- <dxi-tree-list-column
- dataField="BirthDate"
- dataType="date">
- </dxi-tree-list-column>
- <dxi-tree-list-column
- dataField="HireDate"
- dataType="date">
- </dxi-tree-list-column>
- <dxi-tree-list-column dataField="City"></dxi-tree-list-column>
- <dxi-tree-list-column dataField="State"></dxi-tree-list-column>
- <dxi-tree-list-column dataField="Email"></dxi-tree-list-column>
- <dxi-tree-list-column dataField="MobilePhone"></dxi-tree-list-column>
- <dxi-tree-list-column dataField="Skype"></dxi-tree-list-column>
- </dx-tree-list>
Resize Columns
In the default configuration, TreeList columns have equal widths (width is set to "auto"). To change the column layout, set each column's width property or enable columnAutoWidth to adjust all columns to fit cell values. To allow users to change the column layout, enable allowColumnResizing.
- <dx-tree-list
- [allowColumnResizing]="true"
- [columnAutoWidth]="true"
- >
- <dxi-tree-list-column
- dataField="BirthDate"
- dataType="date"
- [width]="100"
- ></dxi-tree-list-column>
- <dxi-tree-list-column
- dataField="HireDate"
- dataType="date"
- [width]="100"
- ></dxi-tree-list-column>
- <!-- ... -->
- </dx-tree-list>
Fix Columns
When the total column width exceeds the UI component width, a horizontal scroll bar appears. To keep specific columns visible, enable columnFixing. Set columns[].fixed to true to fix a column. Set fixedPosition to specify the fixed column position or create a sticky column. Users can also change column fixing options in the TreeList context menu.
- <dx-tree-list>
- <dxo-tree-list-column-fixing [enabled]="true"></dxo-tree-list-column-fixing>
- <dxi-tree-list-column
- dataField="FullName"
- [fixed]="true"
- ></dxi-tree-list-column>
- <!-- ... -->
- </dx-tree-list>
Hide Columns
Read Tutorial: Hide a Column Using the API Read Tutorial: Column Chooser
To hide a TreeList column, set columns[].visible to false. If the columnChooser is enabled, users can restore hidden columns. To hide a column in both the component and the column chooser, omit the corresponding columns[] item.
- <dx-tree-list>
- <dxo-tree-list-column-chooser [enabled]="true"></dxo-tree-list-column-chooser>
- <dxi-tree-list-column
- dataField="Email"
- [visible]="false"
- ></dxi-tree-list-column>
- <!-- ... -->
- </dx-tree-list>
Sort Data
Read Tutorial: TreeList - Sorting
The sorting.mode property specifies whether users can sort TreeList records against single or multiple columns.
You can also set a column's sortOrder and sortIndex properties to specify initial sorting settings. sortIndex applies only in multi-column sort mode.
To sort data and change sort orders in the UI, click column headers. Hold Shift and click to sort data against multiple columns.
- <dx-tree-list>
- <dxo-tree-list-sorting mode="multiple"></dxo-tree-list-sorting>
- <!-- ... -->
- </dx-tree-list>
Filter and Search Data
Read Tutorial: TreeList - Filtering and Searching
TreeList includes the following UI elements used to filter and search data:
This tutorial uses the filterRow and searchPanel:
- <dx-tree-list>
- <dxo-tree-list-filter-row [visible]="true"></dxo-tree-list-filter-row>
- <dxo-tree-list-search-panel [visible]="true"></dxo-tree-list-search-panel>
- <!-- ... -->
- </dx-tree-list>
Edit and Validate Data
Read Tutorial: TreeList - Editing
Users can add new records and update or delete existing records. To allow these operations, enable the following editing options:
TreeList supports multiple edit modes. This tutorial uses the pop-up edit mode.
This tutorial also implements data validation to check that edited data is valid before saving. Required rules are configured to ensure certain column values are never empty:
- <dx-tree-list>
- <dxo-tree-list-editing
- mode="popup"
- [allowUpdating]="true"
- [allowDeleting]="true"
- [allowAdding]="true"
- ></dxo-tree-list-editing>
- <dxi-tree-list-column dataField="FullName">
- <dxi-tree-list-validation-rule type="required"></dxi-tree-list-validation-rule>
- </dxi-tree-list-column>
- <dxi-tree-list-column dataField="Position">
- <dxi-tree-list-validation-rule type="required"></dxi-tree-list-validation-rule>
- </dxi-tree-list-column>
- <dxi-tree-list-column dataField="BirthDate">
- <dxi-tree-list-validation-rule type="required"></dxi-tree-list-validation-rule>
- </dxi-tree-list-column>
- <dxi-tree-list-column dataField="HireDate">
- <dxi-tree-list-validation-rule type="required"></dxi-tree-list-validation-rule>
- </dxi-tree-list-column>
- <dxi-tree-list-column dataField="State">
- <dxi-tree-list-validation-rule type="required"></dxi-tree-list-validation-rule>
- </dxi-tree-list-column>
- <!-- ... -->
- </dx-tree-list>
The TreeList component does not support editing hierarchical data sources out of the box. To configure editing operations for hierarchical data sources, refer to the following example:
Select Records
Read Tutorial: TreeList - Selection
TreeList supports single- and multiple-row selection. To enable row selection, configure the selection.mode property.
Handle onSelectionChanged to obtain the selected records at runtime. This tutorial uses onSelectionChanged to display the selected employee names in an element outside the component:
- <div id="app-container">
- <dx-tree-list (onSelectionChanged)="selectEmployee($event)">
- <dxo-tree-list-selection mode="single"></dxo-tree-list-selection>
- <!-- ... -->
- </dx-tree-list>
- @if (selectedEmployee) {
- <p id="selected-employee">
- Selected employee: {{ selectedEmployee.FullName }}
- </p>
- }
- </div>
- import { Component } from '@angular/core';
- import { DxTreeListTypes } from 'devextreme-angular/ui/tree-list';
- import { Employee, EmployeesService } from './employees.service';
- // ...
- export class AppComponent {
- selectedEmployee: Employee;
- constructor(service: EmployeesService) {
- this.selectEmployee = this.selectEmployee.bind(this);
- }
- selectEmployee(e: DxTreeListTypes.SelectionChangedEvent) {
- e.component.byKey(e.currentSelectedRowKeys[0]).done(employee => {
- if(employee) {
- this.selectedEmployee = employee;
- }
- });
- }
- }
Customize the Toolbar
The TreeList includes an integrated toolbar that displays predefined and custom controls. To add or remove toolbar items, declare the toolbar.items[] array. Toolbar items within the UI preserve the order in which they are declared.
This tutorial illustrates how to add the following items to the toolbar:
Predefined controls
Declare a toolbar item element and specify the name and properties that you want to customize (see the "addRowButton" configuration in the code below). If a control does not need customization, include its name only. Ensure that items[] contain controls for all features that you enabled in your TreeList.DevExtreme components
Configure a DevExtreme component within a toolbar item element. In this tutorial, we extended the toolbar's item collection with a custom Button that expands or collapses all grid records.
- <dx-tree-list
- [autoExpandAll]="expanded"
- [(expandedRowKeys)]="expandedRowKeys"
- >
- <dxo-tree-list-toolbar>
- <dxi-tree-list-item location="after">
- <dx-button
- [text]="expanded ? 'Collapse All' : 'Expand All'"
- [width]="136"
- (onClick)="toggleExpansion()"
- >
- </dx-button>
- </dxi-tree-list-item>
- <dxi-tree-list-item
- name="addRowButton"
- showText="always"
- ></dxi-tree-list-item>
- <dxi-tree-list-item name="exportButton"></dxi-tree-list-item>
- <dxi-tree-list-item name="columnChooserButton"></dxi-tree-list-item>
- <dxi-tree-list-item name="searchPanel"></dxi-tree-list-item>
- </dxo-tree-list-toolbar>
- <!-- ... -->
- </dx-tree-list>
- import { DxButtonModule } from 'devextreme-angular/ui/button';
- // ...
- @Component({
- // ...
- imports: [DxTreeListModule, DxButtonModule]
- })
- export class AppComponent {
- expanded: boolean = true;
- expandedRowKeys: number[] = [];
- toggleExpansion(): void {
- this.expanded = !this.expanded;
- this.expandedRowKeys = [];
- }
- }
Enable Row Drag & Drop
TreeList allows users to drag and drop rows to reorder records or modify the node hierarchy. To configure this feature, specify the following rowDragging options:
- <dx-tree-list>
- <dxo-tree-list-row-dragging
- [allowDropInsideItem]="true"
- [allowReordering]="true"
- [onDragChange]="onDragChange"
- [onReorder]="onReorder"
- ></dxo-tree-list-row-dragging>
- <!-- ... -->
- </dx-tree-list>
- import { inject, signal } from '@angular/core';
- import { type DxTreeListTypes } from 'devextreme-angular/ui/tree-list';
- import { EmployeesService, type Employee } from './employees.service';
- // ...
- export class AppComponent {
- private readonly employeesService = inject(EmployeesService);
- employees = signal<Employee[]>(this.employeesService.getEmployees());
- onDragChange = (e: DxTreeListTypes.RowDraggingChangeEvent): void => {
- const visibleRows = e.component.getVisibleRows();
- const sourceNode = e.component.getNodeByKey(e.itemData.ID);
- let targetNode = visibleRows[e.toIndex].node;
- while (targetNode?.data) {
- if (targetNode.data.ID === sourceNode.data.ID) {
- e.cancel = true;
- break;
- }
- const parentNode = targetNode.parent;
- if (!parentNode) {
- break;
- }
- targetNode = parentNode;
- }
- };
- onReorder = (e: DxTreeListTypes.RowDraggingReorderEvent): void => {
- const visibleRows = e.component.getVisibleRows();
- const sourceData = e.itemData;
- const targetData = visibleRows[e.toIndex].data;
- if (e.dropInsideItem) {
- e.itemData.HeadID = targetData.ID;
- e.component.refresh().catch(() => {
- // Handle error silently
- });
- } else {
- let targetIndex = this.employees().indexOf(targetData);
- if (sourceData.HeadID !== targetData.HeadID) {
- sourceData.HeadID = targetData.HeadID;
- if (e.toIndex > e.fromIndex) {
- targetIndex += 1;
- }
- }
- this.employeesService.reorderEmployees(sourceData, targetIndex);
- this.employees.set([...this.employeesService.getEmployees()]);
- }
- };
- }
- // ...
- export class EmployeesService {
- reorderEmployees(sourceData: Employee, targetIndex: number): void {
- const sourceIndex = this.employees.indexOf(sourceData);
- this.employees.splice(sourceIndex, 1);
- this.employees.splice(targetIndex, 0, sourceData);
- }
- }
Enable Pagination
Read Tutorial: TreeList - Paging
TreeList can load data in pages. To configure pagination, set paging.enabled to true and configure the pageSize property:
- <dx-tree-list>
- <dxo-tree-list-paging
- [enabled]="true"
- [pageSize]="12"
- ></dxo-tree-list-paging>
- <!-- ... -->
- </dx-tree-list>
For further information on the TreeList component, refer to the following resources: