Search results

Getting started with React Dropdown Tree component

This section explains you about how to create a simple Dropdown Tree component and configure its available functionalities in React.

Dependencies

The following list of dependencies are required to use the Dropdown Tree component in your application.

|-- @syncfusion/ej2-react-dropdowns
    |-- @syncfusion/ej2-base
    |-- @syncfusion/ej2-data
    |-- @syncfusion/ej2-react-base
    |-- @syncfusion/ej2-dropdowns
        |-- @syncfusion/ej2-lists
        |-- @syncfusion/ej2-inputs
        |-- @syncfusion/ej2-navigations
        |-- @syncfusion/ej2-popups
            |-- @syncfusion/ej2-buttons

Installation and configuration

You can use Create-react-app to setup the applications.

To install create-react-app run the following command.

npm install -g create-react-app

Start a new project using create-react-app command as follows

Using Tsx

create-react-app quickstart --scripts-version=react-scripts-ts
cd quickstart

Using Jsx

create-react-app quickstart

cd quickstart

Adding syncfusion packages

All the available Essential JS 2 packages are published in npmjs.com public registry. You can choose the component that you want to install.

To install Dropdown Tree component, use the following command

npm install @syncfusion/ej2-react-dropdowns –save

Adding Dropdown Tree component

Now, you can start adding Dropdown Tree component in the application. For getting started, add the Dropdown Tree component in src/App.tsx file using following code. Add the below code in the src/App.tsx to initialize the Dropdown Tree.

import { DropDownTreeComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from "react-dom";

export default class App extends React.Component<{}, {}> {
  public render() {
    return (
       // specifies the tag for render the DropDownTree component
      <DropDownTreeComponent id='dropdowntree'/>
    );
  }
}

ReactDOM.render(<App />, document.getElementById('sample'));
import { DropDownTreeComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from "react-dom";
export default class App extends React.Component {
    render() {
        return (
        // specifies the tag for render the DropDownTree component
        <DropDownTreeComponent id='dropdowntree'/>);
    }
}
ReactDOM.render(<App />, document.getElementById('sample'));

Adding CSS reference

Import the Dropdown Tree component required CSS references as follows in src/App.css.

/* import the Dropdown Tree dependency styles */

@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/material.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/material.css";
@import "../node_modules/@syncfusion/ej2-react-dropdowns/styles/material.css";

Binding data source

The Dropdown Tree component can load the data either from local data sources or remote data services. This can be done using the dataSource property that is a member of the fields property. The dataSource property supports array of JavaScript objects and DataManager. Here, an array of JSON values is passed to the Dropdown Tree component.

import { DropDownTreeComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

export default class App extends React.Component<{}, {}> {
  // definining the dataSource
  private data: { [key: string]: Object }[] = [
     {
        nodeId: '01', nodeText: 'Music',
        nodeChild: [
            { nodeId: '01-01', nodeText: 'Gouttes.mp3' }
        ]
     },
     {
        nodeId: '02', nodeText: 'Videos', expanded: true,
        nodeChild: [
            { nodeId: '02-01', nodeText: 'Naturals.mp4' },
            { nodeId: '02-02', nodeText: 'Wild.mpeg' },
        ]
     },
     {
        nodeId: '03', nodeText: 'Documents',
        nodeChild: [
            { nodeId: '03-01', nodeText: 'Environment Pollution.docx' },
            { nodeId: '03-02', nodeText: 'Global Water, Sanitation, & Hygiene.docx' },
            { nodeId: '03-03', nodeText: 'Global Warming.ppt' },
            { nodeId: '03-04', nodeText: 'Social Network.pdf' },
            { nodeId: '03-05', nodeText: 'Youth Empowerment.pdf' },
        ]
     },
    ];
    private fields: Object = { dataSource: this.data, value: 'nodeId', text: 'nodeText', child: 'nodeChild' };
  public render() {
    return (
        // specifies the tag for render the DropDownTree component
      <DropDownTreeComponent id="dropdowntree" fields={this.fields} />
    );
  }
}
ReactDOM.render(<App />, document.getElementById('sample'));
import { DropDownTreeComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
export default class App extends React.Component {
    constructor() {
        super(...arguments);
        // definining the dataSource
        this.data = [
            {
                nodeId: '01', nodeText: 'Music',
                nodeChild: [
                    { nodeId: '01-01', nodeText: 'Gouttes.mp3' }
                ]
            },
            {
                nodeId: '02', nodeText: 'Videos', expanded: true,
                nodeChild: [
                    { nodeId: '02-01', nodeText: 'Naturals.mp4' },
                    { nodeId: '02-02', nodeText: 'Wild.mpeg' },
                ]
            },
            {
                nodeId: '03', nodeText: 'Documents',
                nodeChild: [
                    { nodeId: '03-01', nodeText: 'Environment Pollution.docx' },
                    { nodeId: '03-02', nodeText: 'Global Water, Sanitation, & Hygiene.docx' },
                    { nodeId: '03-03', nodeText: 'Global Warming.ppt' },
                    { nodeId: '03-04', nodeText: 'Social Network.pdf' },
                    { nodeId: '03-05', nodeText: 'Youth Empowerment.pdf' },
                ]
            },
        ];
        this.fields = { dataSource: this.data, value: 'nodeId', text: 'nodeText', child: 'nodeChild' };
    }
    render() {
        return (
        // specifies the tag for render the DropDownTree component
        <DropDownTreeComponent id="dropdowntree" fields={this.fields}/>);
    }
}
ReactDOM.render(<App />, document.getElementById('sample'));

Run the application

After completing the configuration required to render a basic Dropdown Tree, run the following command to display the output in your default browser.

npm start
Source
Preview
index.tsx
index.jsx
import { DropDownTreeComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

export default class App extends React.Component<{}, {}> {
  // definining the dataSource
  private data: { [key: string]: Object }[] = [
     {
        nodeId: '01', nodeText: 'Music',
        nodeChild: [
            { nodeId: '01-01', nodeText: 'Gouttes.mp3' }
        ]
     },
     {
        nodeId: '02', nodeText: 'Videos', expanded: true,
        nodeChild: [
            { nodeId: '02-01', nodeText: 'Naturals.mp4' },
            { nodeId: '02-02', nodeText: 'Wild.mpeg' },
        ]
     },
     {
        nodeId: '03', nodeText: 'Documents',
        nodeChild: [
            { nodeId: '03-01', nodeText: 'Environment Pollution.docx' },
            { nodeId: '03-02', nodeText: 'Global Water, Sanitation, & Hygiene.docx' },
            { nodeId: '03-03', nodeText: 'Global Warming.ppt' },
            { nodeId: '03-04', nodeText: 'Social Network.pdf' },
            { nodeId: '03-05', nodeText: 'Youth Empowerment.pdf' },
        ]
     },
    ];
    private fields: Object = { dataSource: this.data, value: 'nodeId', text: 'nodeText', child: 'nodeChild' };
  public render() {
    return (
        // specifies the tag for render the DropDownTree component
      <DropDownTreeComponent id="dropdowntree" fields={this.fields} />
    );
  }
}
ReactDOM.render(<App />, document.getElementById('sample'));
import { DropDownTreeComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
export default class App extends React.Component {
    constructor() {
        super(...arguments);
        // definining the dataSource
        this.data = [
            {
                nodeId: '01', nodeText: 'Music',
                nodeChild: [
                    { nodeId: '01-01', nodeText: 'Gouttes.mp3' }
                ]
            },
            {
                nodeId: '02', nodeText: 'Videos', expanded: true,
                nodeChild: [
                    { nodeId: '02-01', nodeText: 'Naturals.mp4' },
                    { nodeId: '02-02', nodeText: 'Wild.mpeg' },
                ]
            },
            {
                nodeId: '03', nodeText: 'Documents',
                nodeChild: [
                    { nodeId: '03-01', nodeText: 'Environment Pollution.docx' },
                    { nodeId: '03-02', nodeText: 'Global Water, Sanitation, & Hygiene.docx' },
                    { nodeId: '03-03', nodeText: 'Global Warming.ppt' },
                    { nodeId: '03-04', nodeText: 'Social Network.pdf' },
                    { nodeId: '03-05', nodeText: 'Youth Empowerment.pdf' },
                ]
            },
        ];
        this.fields = { dataSource: this.data, value: 'nodeId', text: 'nodeText', child: 'nodeChild' };
    }
    render() {
        return (
        // specifies the tag for render the DropDownTree component
        <DropDownTreeComponent id="dropdowntree" fields={this.fields}/>);
    }
}
ReactDOM.render(<App />, document.getElementById('sample'));