React TreeView - Expand and Collapse Nodes

Initially

If a node is supposed to be expanded initially, set its expanded field to true. If another field specifies whether a node is expanded or collapsed, assign its name to the expandedExpr property as shown in the following code.

App.js
  • import React from 'react';
  •  
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import TreeView from 'devextreme-react/tree-view';
  •  
  • const hierarchicalData = [{
  • key: '1',
  • name: 'Fruits',
  • isExpanded: true,
  • items: [
  • { key: '1_1', name: 'Apples' },
  • { key: '1_2', name: 'Oranges' }
  • ]
  • }, {
  • key: '2',
  • name: 'Vegetables',
  • isExpanded: true,
  • items: [
  • { key: '2_1', name: 'Cucumbers' },
  • { key: '2_2', name: 'Tomatoes' }
  • ]
  • }];
  •  
  • class App extends React.Component {
  • render() {
  • return (
  • <TreeView
  • keyExpr="key"
  • displayExpr="name"
  • expandedExpr="isExpanded"
  • items={hierarchicalData} />
  • );
  • }
  • }
  •  
  • export default App;

Using the API

The TreeView provides the following API to expand and collapse nodes:

  • All nodes
    You can use the expandAll() and collapseAll() methods to expand and collapse nodes at once. Note that the expandAll() method expands only the loaded nodes if data is loaded on demand.

    App.js
    • import React from 'react';
    •  
    • import 'devextreme/dist/css/dx.light.css';
    •  
    • import TreeView from 'devextreme-react/tree-view';
    •  
    • const data = [ ... ];
    •  
    • class App extends React.Component {
    • constructor() {
    • super();
    • this.treeViewRef = React.createRef();
    • this.expandAllNodes = this.expandAllNodes.bind(this);
    • this.collapseAllNodes = this.collapseAllNodes.bind(this);
    • }
    • render() {
    • return (
    • <TreeView
    • items={data}
    • ref={this.treeViewRef} />
    • );
    • }
    • expandAllNodes() {
    • this.treeView.expandAll();
    • }
    • collapseAllNodes() {
    • this.treeView.collapseAll();
    • }
    • get treeView() {
    • return this.treeViewRef.current.instance();
    • }
    • }
    •  
    • export default App;
  • Individual nodes
    Call the expandItem(key) or collapseItem(key) method and pass a node key as an argument:

    App.js
    • import React from 'react';
    •  
    • import 'devextreme/dist/css/dx.light.css';
    •  
    • import TreeView from 'devextreme-react/tree-view';
    • const data = [ ... ];
    •  
    • class App extends React.Component {
    • constructor() {
    • super();
    • this.treeViewRef = React.createRef();
    • this.expandNode = this.expandNode.bind(this);
    • this.collapseNode = this.collapseNode.bind(this);
    • }
    • render() {
    • return (
    • <TreeView
    • items={data}
    • ref={this.treeViewRef} />
    • );
    • }
    • expandNode(key) {
    • this.treeView.expandItem(key);
    • }
    • collapseNode(key) {
    • this.treeView.collapseItem(key);
    • }
    • get treeView() {
    • return this.treeViewRef.current.instance();
    • }
    • }
    •  
    • export default App;

Events

To execute certain commands when a node is expanded or collapsed, handle the itemExpanded or itemCollapsed event. Assign event handling functions to the onItemExpanded or onItemCollapsed property when you configure the UI component if these functions are going to remain unchanged.

App.js
  • import React from 'react';
  •  
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import TreeView from 'devextreme-react/tree-view';
  •  
  • const data = [ ... ];
  •  
  • class App extends React.Component {
  • render() {
  • return (
  • <TreeView
  • onItemExpanded={this.onItemExpanded}
  • onItemCollapsed={this.onItemCollapsed}
  • dataSource={data} />
  • );
  • }
  •  
  • onItemExpanded(e) {
  • // Handler of the 'itemExpanded' event
  • }
  •  
  • onItemCollapsed(e) {
  • // Handler of the 'itemCollapsed' event
  • }
  • }
  •  
  • export default App;
See Also