React 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 React application and use the following code to create a TreeList:
- import React from 'react';
- import 'devextreme/dist/css/dx.fluent.blue.light.css';
- import { TreeList } from 'devextreme-react/tree-list';
- function App() {
- return (
- <div className="App">
- <TreeList id="tree-list">
- {/* Configuration goes here */}
- </TreeList>
- </div>
- );
- }
- export default App;
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:
- import React, { useState } from 'react';
- import { TreeList } from 'devextreme-react/tree-list';
- import { employeesService, type Employee } from './employeesService';
- function App() {
- const [currentEmployees, setCurrentEmployees] = useState<Employee[]>(employeesService.getEmployees());
- return (
- <div className="App">
- <TreeList
- dataSource={currentEmployees}
- rootValue={-1}
- keyExpr="ID"
- parentIdExpr="HeadID">
- </TreeList>
- </div>
- );
- }
- 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;
- }
- const employees: Employee[] = [
- // ...
- ];
- let employeeData = [...employees];
- export const employeesService = {
- getEmployees: (): Employee[] => employeeData,
- // ...
- }
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:
- import { TreeList } from 'devextreme-react/tree-list';
- function App() {
- return (
- <TreeList
- autoExpandAll={true}
- >
- {/* ... */}
- </TreeList>
- );
- }
Customize Columns
Read Tutorial: Columns - Overview
Specify the columns array to define and customize TreeList columns. To add a column, specify the 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:
- import { TreeList, Column } from 'devextreme-react/tree-list';
- function App() {
- return (
- <TreeList allowColumnReordering={true}>
- <Column dataField="FullName" />
- <Column dataField="Position" />
- <Column
- dataField="BirthDate"
- dataType="date">
- </Column>
- <Column
- dataField="HireDate"
- dataType="date">
- </Column>
- <Column dataField="City" />
- <Column dataField="State" />
- <Column dataField="Email" />
- <Column dataField="MobilePhone" />
- <Column dataField="Skype" />
- </TreeList>
- );
- }
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.
- import { TreeList, Column } from 'devextreme-react/tree-list';
- function App() {
- return (
- <TreeList
- columnAutoWidth={true}
- allowColumnResizing={true}
- >
- <Column
- dataField="BirthDate"
- dataType="date"
- width={100}
- />
- <Column
- dataField="HireDate"
- dataType="date"
- width={100}
- />
- {/* ... */}
- </TreeList>
- );
- }
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.
- import { TreeList, ColumnFixing, Column } from 'devextreme-react/tree-list';
- function App() {
- return (
- <div className="App">
- <TreeList>
- <ColumnFixing enabled={true} />
- <Column
- dataField="FullName"
- fixed={true}
- />
- {/* ... */}
- </TreeList>
- </div>
- );
- }
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.
- import { TreeList, ColumnChooser, Column } from 'devextreme-react/tree-list';
- function App() {
- return (
- <TreeList ... >
- <ColumnChooser enabled={true} />
- <Column
- dataField="Email"
- visible={false}
- />
- {/* ... */}
- </TreeList>
- );
- }
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.
- import { TreeList, Sorting, Column } from 'devextreme-react/tree-list';
- function App() {
- return (
- <div className="App">
- <TreeList ... >
- <Sorting mode="multiple" />
- {/* ... */}
- </TreeList>
- </div>
- );
- }
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:
- import { TreeList, FilterRow, SearchPanel } from 'devextreme-react/tree-list';
- function App() {
- return (
- <TreeList ... >
- <FilterRow visible={true} />
- <SearchPanel visible={true} />
- {/* ... */}
- </TreeList>
- );
- }
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:
- import { TreeList, Editing, Column, RequiredRule } from 'devextreme-react/tree-list';
- function App() {
- return (
- <TreeList>
- <Editing
- mode="popup"
- allowUpdating={true}
- allowDeleting={true}
- allowAdding={true}
- />
- <Column dataField="FullName">
- <RequiredRule />
- </Column>
- <Column dataField="Position">
- <RequiredRule />
- </Column>
- <Column dataField="BirthDate">
- <RequiredRule />
- </Column>
- <Column dataField="HireDate">
- <RequiredRule />
- </Column>
- <Column dataField="State">
- <RequiredRule />
- </Column>
- {/* ... */}
- </TreeList>
- );
- }
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:
- import React, { useCallback, useState } from 'react';
- import { TreeList, Selection, type TreeListTypes } from 'devextreme-react/tree-list';
- import { type Employee } from './employees';
- function SelectedEmployee(props) {
- if(props.employee) {
- return (
- <p id="selected-employee">
- Selected employee: {props.employee.FullName}
- </p>
- );
- }
- return null;
- }
- function App() {
- const [selectedEmployee, setSelectedEmployee] = useState();
- const selectEmployee = useCallback((e: TreeListTypes.SelectionChangedEvent): void => {
- e.component.byKey(e.currentSelectedRowKeys[0]).then((employee: Employee) => {
- setSelectedEmployee(employee);
- }).catch(() => {});
- }, []);
- return (
- <div className="App">
- <TreeList onSelectionChanged={selectEmployee}>
- <Selection mode="single" />
- {/* ... */}
- </TreeList>
- <SelectedEmployee employee={selectedEmployee} />
- </div>
- );
- }
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.
- import React, { useCallback, useState } from 'react';
- import { TreeList, Toolbar, Item } from 'devextreme-react/tree-list';
- import { Button } from 'devextreme-react/button';
- function App() {
- const [expanded, setExpanded] = useState(true);
- const [expandedRowKeys, setExpandedRowKeys] = useState<number[]>([]);
- const toggleExpansion = useCallback(() => {
- setExpanded((prevExpanded: boolean) => !prevExpanded);
- setExpandedRowKeys([]);
- }, []);
- return (
- <TreeList
- autoExpandAll={expanded}
- expandedRowKeys={expandedRowKeys}
- >
- <Toolbar>
- <Item location="after">
- <Button
- text={expanded ? 'Collapse All' : 'Expand All'}
- width={136}
- onClick={toggleExpansion}
- />
- </Item>
- <Item name="addRowButton" showText="always" />
- <Item name="exportButton" />
- <Item name="columnChooserButton" />
- <Item name="searchPanel" />
- </Toolbar>
- {/* ... */}
- </TreeList>
- );
- }
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:
- import React, { useCallback, useState } from 'react';
- import { TreeList, RowDragging, type TreeListTypes } from 'devextreme-react/tree-list';
- import { employeesService, type Employee } from './employeesService';
- function App() {
- const [currentEmployees, setCurrentEmployees] = useState<Employee[]>(employeesService.getEmployees());
- const onDragChange = useCallback((e: any) => {
- const visibleRows = e.component.getVisibleRows();
- const sourceNode = e.component.getNodeByKey(e.itemData.ID);
- let targetNode: TreeListTypes.Node<Employee, number> | undefined = visibleRows[e.toIndex].node;
- while (targetNode?.data) {
- if (targetNode.data.ID === sourceNode.data.ID) {
- e.cancel = true;
- break;
- }
- targetNode = targetNode.parent;
- }
- }, []);
- const onReorder = useCallback((e: any) => {
- const visibleRows = e.component.getVisibleRows();
- const sourceData = e.itemData;
- const targetData = visibleRows[e.toIndex].data;
- if (e.dropInsideItem) {
- const updatedSourceData = { ...sourceData, HeadID: targetData.ID };
- employeesService.updateEmployee(updatedSourceData);
- } else {
- let targetIndex = currentEmployees.indexOf(targetData);
- if (sourceData.HeadID !== targetData.HeadID) {
- sourceData.HeadID = targetData.HeadID;
- if (e.toIndex > e.fromIndex) {
- targetIndex += 1;
- }
- }
- employeesService.reorderEmployees(sourceData, targetIndex);
- }
- setCurrentEmployees(employeesService.getEmployees());
- e.component.refresh();
- }, [currentEmployees]);
- return (
- <TreeList>
- <RowDragging
- onDragChange={onDragChange}
- onReorder={onReorder}
- allowDropInsideItem={true}
- allowReordering={true}
- />
- {/* ... */}
- </TreeList>
- );
- }
- let employeeData = [...employees];
- export const employeesService = {
- reorderEmployees: (sourceData: Employee, targetIndex: number): void => {
- const sourceIndex = employeeData.indexOf(sourceData);
- employeeData.splice(sourceIndex, 1);
- employeeData.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:
- import { TreeList, Paging } from 'devextreme-react/tree-list';
- function App() {
- return (
- <TreeList>
- <Paging
- enabled={true}
- defaultPageSize={12}
- />
- {/* ... */}
- </TreeList>
- );
- }
For further information on the TreeList component, refer to the following resources: