Search results

Templates in React Dropdown Tree component

12 Aug 2022 / 9 minutes to read

The Dropdown Tree provides support to customize each list item, header, and footer elements.

Item template

The content of each list item within the Dropdown Tree can be customized with the help of itemTemplate property.

In the following sample, the Dropdown Tree list items are customized with employee information such as name and job using the itemTemplate property.

The template expression should be provided inside the {…} interpolation syntax.

Source
Preview
index.tsx
index.html
styles.css
Copied to clipboard
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<{}, {}> {
  private data: { [key: string]: Object }[] = [
    { "id": 1, "name": "Steven Buchanan",  "job": "General Manager", "hasChild": true, "expanded": true },
    { "id": 2, "pid": 1, "name": "Laura Callahan",  "job": "Product Manager", "hasChild": true },
    { "id": 3, "pid": 2, "name": "Andrew Fuller",  "job": "Team Lead", "hasChild": true },
    { "id": 4, "pid": 3, "name": "Anne Dodsworth",  "job": "Developer" },
    { "id": 10, "pid": 3, "name": "Lilly",  "job": "Developer", "status":"online" },
    { "id": 5, "pid": 1, "name": "Nancy Davolio",  "job": "Product Manager", "hasChild": true},
    { "id": 6, "pid": 5, "name": "Michael Suyama",  "job": "Team Lead", "hasChild": true },
    { "id": 7, "pid": 6, "name": "Robert King",  "job": "Developer " },
    { "id": 11, "pid": 6, "name": "Mary", "job": "Developer " },
    { "id": 9, "pid": 1, "name": "Janet Leverling",  "job": "HR" }
  ];
  private fields: Object = { dataSource: this.data, value: 'id', text: 'name', parentValue: "pid", hasChildren: 'hasChild' };
  private itemTemplate(data: any): JSX.Element {
    return (<div><span className="ename"> {data.name} - </span><span className="ejob"> {data.job} </span></div>);
  }
  public render() {
    return (<DropDownTreeComponent fields={this.fields} placeholder="Select an employee" itemTemplate={this.itemTemplate} popupHeight="270px" cssClass="custom" width="100%"/>);
  }
}
ReactDOM.render(<App />, document.getElementById('sample'));
Copied to clipboard
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Syncfusion React Dropdown Tree</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Essential JS 2 for React Components" />
    <meta name="author" content="Syncfusion" />
    <link href="//cdn.syncfusion.com/ej2/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-buttons/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-navigations/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-react-inputs/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-react-dropdowns/styles/material.css" rel="stylesheet" />
	<link href="./styles.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
    <style>
        #loader {
            color: #008cff;
            height: 40px;
            left: 45%;
            position: absolute;
            top: 45%;
            width: 30%;
        }
    </style>
</head>

<body>
    <div id='sample' style="margin: 20px auto 0; width:250px;">
        <div id='loader'>Loading....</div>
    </div>
</body>

</html>
Copied to clipboard
.custom .ejob{
    opacity: .60;
}

Header template

The header element is shown statically at the top of the popup list items within the Dropdown Tree. A custom element can be placed as a header element using the headerTemplate property.

In the following sample, the header is customized with the custom element.

Source
Preview
index.tsx
index.html
styles.css
Copied to clipboard
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<{}, {}> {
  private data: { [key: string]: Object }[] = [
    { "id": 1, "name": "Steven Buchanan",  "job": "General Manager", "hasChild": true, "expanded": true },
    { "id": 2, "pid": 1, "name": "Laura Callahan",  "job": "Product Manager", "hasChild": true },
    { "id": 3, "pid": 2, "name": "Andrew Fuller",  "job": "Team Lead", "hasChild": true },
    { "id": 4, "pid": 3, "name": "Anne Dodsworth",  "job": "Developer" },
    { "id": 10, "pid": 3, "name": "Lilly",  "job": "Developer", "status":"online" },
    { "id": 5, "pid": 1, "name": "Nancy Davolio",  "job": "Product Manager", "hasChild": true},
    { "id": 6, "pid": 5, "name": "Michael Suyama",  "job": "Team Lead", "hasChild": true },
    { "id": 7, "pid": 6, "name": "Robert King",  "job": "Developer " },
    { "id": 11, "pid": 6, "name": "Mary", "job": "Developer " },
    { "id": 9, "pid": 1, "name": "Janet Leverling",  "job": "HR" }
  ];
  private fields: Object = { dataSource: this.data, value: 'id', text: 'name', parentValue: "pid", hasChildren: 'hasChild'};
  private headerTemplate(data: any): JSX.Element {
    return (<div className="head"> Employee List </div>);
  }
  public render() {
    return (<DropDownTreeComponent fields={this.fields} placeholder="Select an employee"  headerTemplate={this.headerTemplate} popupHeight="270px" cssClass="custom"/>);
  }
}
ReactDOM.render(<App />, document.getElementById('sample'));
Copied to clipboard
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Syncfusion React Dropdown Tree</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Essential JS 2 for React Components" />
    <meta name="author" content="Syncfusion" />
    <link href="//cdn.syncfusion.com/ej2/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-buttons/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-navigations/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-react-inputs/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-react-dropdowns/styles/material.css" rel="stylesheet" />
	<link href="./styles.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
    <style>
        #loader {
            color: #008cff;
            height: 40px;
            left: 45%;
            position: absolute;
            top: 45%;
            width: 30%;
        }
    </style>
</head>

<body>
    <div id='sample' style="margin: 20px auto 0; width:250px;">
        <div id='loader'>Loading....</div>
    </div>
</body>

</html>
Copied to clipboard
.custom .head {
    height: 40px;
    line-height: 40px;
    font-size: 14px;
    margin: 0 auto;
    width: 200px;
    padding: 0 20px;
    font-weight: bold;
    border-bottom: 1px solid #e0e0e0;
}

The Dropdown Tree has options to show a footer element at the bottom of the list items in the popup list. Here, you can place any custom element as a footer element using the footerTemplate property.

In the following sample, the footer element displays the total number of employees present in the Dropdown Tree.

Source
Preview
index.tsx
index.html
styles.css
Copied to clipboard
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<{}, {}> {
  private data: { [key: string]: Object }[] = [
    { "id": 1, "name": "Steven Buchanan",  "job": "General Manager", "hasChild": true, "expanded": true },
    { "id": 2, "pid": 1, "name": "Laura Callahan",  "job": "Product Manager", "hasChild": true },
    { "id": 3, "pid": 2, "name": "Andrew Fuller",  "job": "Team Lead", "hasChild": true },
    { "id": 4, "pid": 3, "name": "Anne Dodsworth",  "job": "Developer" },
    { "id": 10, "pid": 3, "name": "Lilly",  "job": "Developer", "status":"online" },
    { "id": 5, "pid": 1, "name": "Nancy Davolio",  "job": "Product Manager", "hasChild": true},
    { "id": 6, "pid": 5, "name": "Michael Suyama",  "job": "Team Lead", "hasChild": true },
    { "id": 7, "pid": 6, "name": "Robert King",  "job": "Developer " },
    { "id": 11, "pid": 6, "name": "Mary", "job": "Developer " },
    { "id": 9, "pid": 1, "name": "Janet Leverling",  "job": "HR" }
  ];
  private fields: object =  { dataSource: this.data, value: 'id', text: 'name', parentValue: "pid", hasChildren: 'hasChild' };
  private footerTemplate(data: any): JSX.Element {
    return (<div class='foot'> Total number of employees: 10 </div>);
  }
  public render() {
    return (
            <DropDownTreeComponent fields={this.fields} placeholder="Select an employee" footerTemplate={this.footerTemplate} popupHeight="270px" cssClass="custom"/>);
  }
}
ReactDOM.render(<App />, document.getElementById('sample'));
Copied to clipboard
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Syncfusion React Dropdown Tree</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Essential JS 2 for React Components" />
    <meta name="author" content="Syncfusion" />
    <link href="//cdn.syncfusion.com/ej2/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-buttons/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-navigations/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-react-inputs/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-react-dropdowns/styles/material.css" rel="stylesheet" />
	<link href="./styles.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
    <style>
        #loader {
            color: #008cff;
            height: 40px;
            left: 45%;
            position: absolute;
            top: 45%;
            width: 30%;
        }
    </style>
</head>

<body>
    <div id='sample' style="margin: 20px auto 0; width:250px;">
        <div id='loader'>Loading....</div>
    </div>
</body>

</html>
Copied to clipboard
.custom .foot{
    height: 40px;
    line-height: 40px;
    font-size: 14px;
    margin: 0 auto;
    padding: 0 20px;
    width: 100%;
    font-weight: bold;  
}
.custom .e-ddt-footer{
    border-top: 1px solid #e0e0e0;
}

No records template

The Dropdown Tree is supports to display custom design in the popup list content using the noRecordsTemplate property when no matches found on search.

In the following sample, popup list content displays the notification of no data available.

Source
Preview
index.tsx
index.html
Copied to clipboard
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<{}, {}> {
  private data: { [key: string]: Object }[] = [ ];
  private fields: object = { dataSource: this.data, value: 'id', text: 'name', parentValue: "pid", hasChildren: 'hasChild' };
  private noRecordsTemplate(data: any): JSX.Element {
    return (<div class='norecord'> NO DATA AVAILABLE </div>)
  }
  public render() {
    return (
      <DropDownTreeComponent fields={this.fields} placeholder="Select an employee" noRecordsTemplate={this.noRecordsTemplate}  popupHeight="270px"/>);
  }
}
ReactDOM.render(<App />, document.getElementById('sample'));
Copied to clipboard
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Syncfusion React Dropdown Tree</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Essential JS 2 for React Components" />
    <meta name="author" content="Syncfusion" />
    <link href="//cdn.syncfusion.com/ej2/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-buttons/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-navigations/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-react-inputs/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-react-dropdowns/styles/material.css" rel="stylesheet" />
	<link href="./styles.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
    <style>
        #loader {
            color: #008cff;
            height: 40px;
            left: 45%;
            position: absolute;
            top: 45%;
            width: 30%;
        }
    </style>
</head>

<body>
    <div id='sample' style="margin: 20px auto 0; width:250px;">
        <div id='loader'>Loading....</div>
    </div>
</body>

</html>

Action failure template

The Dropdown Tree provides an option to custom design the popup list content using actionFailureTemplate property, when the data fetch request fails at the remote server.

In the following sample, when the data fetch request fails, the Dropdown Tree displays the notification.

Source
Preview
index.tsx
index.html
Copied to clipboard
import { DataManager, ODataV4Adaptor, Query } from '@syncfusion/ej2-data';
import { DropDownTreeComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
export default class RemoteData  extends React.Component<{}, {}> {
  private data: DataManager = new DataManager({
    url: 'https://services.odata.org/V4/Northwind/Northwind.svcs',
    adaptor: new ODataV4Adaptor,
    crossDomain: true, });
  private query: Query = new Query().from('Employees').select('EmployeeID,FirstName,Title').take(5);
  private query1: Query = new Query().from('Orders').select('OrderID,EmployeeID,ShipName').take(5);
  private fields: object = {
    dataSource: this.data, query: this.query, value: 'EmployeeID', text: 'FirstName', hasChildren: 'EmployeeID',
    child: { dataSource: this.data, query: this.query1, value: 'OrderID', parentValue: 'EmployeeID', text: 'ShipName' }
  };
  private actionFailureTemplate (data: any): JSX.Element {
    return (<div class='action-failure'> Data fetch request fails </div>);
  }
  public render() {
    return (
      <DropDownTreeComponent fields={this.fields} actionFailureTemplate={this.actionFailureTemplate} placeholder="Select an employee" popupHeight="200px"/>);
  }
}
ReactDOM.render(<RemoteData  />, document.getElementById('sample'));
Copied to clipboard
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Syncfusion React Dropdown Tree</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Essential JS 2 for React Components" />
    <meta name="author" content="Syncfusion" />
    <link href="//cdn.syncfusion.com/ej2/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-buttons/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-navigations/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-react-inputs/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-react-dropdowns/styles/material.css" rel="stylesheet" />
	<link href="./styles.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
    <style>
        #loader {
            color: #008cff;
            height: 40px;
            left: 45%;
            position: absolute;
            top: 45%;
            width: 30%;
        }
    </style>
</head>

<body>
    <div id='sample' style="margin: 20px auto 0; width:250px;">
        <div id='loader'>Loading....</div>
    </div>
</body>

</html>

Custom template to show selected items in input

In Dropdown Tree, while selecting more than one items via checkbox or multi selection support, all the selected items will be displayed in the input. Instead of displaying all the selected item text, the custom template can be displayed by setting the the mode property as Custom and customTemplate property.

When the mode property is set as Custom, the Dropdown Tree displays the default template value (${value.length} item(s) selected) like 1 item(s) selected or 2 item(s) selected. The default template can be customized by setting customTemplate property.

In the following sample, the Dropdown Tree is rendered with default value of the customTemplate property like “1 item(s) selected or 2 item(s) selected”.

Source
Preview
index.tsx
index.html
Copied to clipboard
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<{}, {}> {
  private data: { [key: string]: Object }[] = [
    { "id": 1, "name": "Steven Buchanan",  "job": "General Manager", "hasChild": true, "expanded": true },
    { "id": 2, "pid": 1, "name": "Laura Callahan",  "job": "Product Manager", "hasChild": true },
    { "id": 3, "pid": 2, "name": "Andrew Fuller",  "job": "Team Lead", "hasChild": true },
    { "id": 4, "pid": 3, "name": "Anne Dodsworth",  "job": "Developer" },
    { "id": 10, "pid": 3, "name": "Lilly",  "job": "Developer", "status":"online" },
    { "id": 5, "pid": 1, "name": "Nancy Davolio",  "job": "Product Manager", "hasChild": true},
    { "id": 6, "pid": 5, "name": "Michael Suyama",  "job": "Team Lead", "hasChild": true },
    { "id": 7, "pid": 6, "name": "Robert King",  "job": "Developer " },
    { "id": 11, "pid": 6, "name": "Mary", "job": "Developer " },
    { "id": 9, "pid": 1, "name": "Janet Leverling",  "job": "HR" }
  ];
  private fields: object = { dataSource: this.data, value: 'id', text: 'name', parentValue: "pid", hasChildren: 'hasChild' };
  private showCheckBox: boolean = true;
  private treeSettings: Object = { autoCheck: true };
  private customTemplate: string = "${value.length} item(s) selected";
  public render() {
    return (<DropDownTreeComponent fields={this.fields} treeSettings={this.treeSettings} customTemplate={this.customTemplate} showCheckBox={this.showCheckBox} mode="Custom" placeholder="Select an employee" popupHeight="200px"/>);
  }
}
ReactDOM.render(<App />, document.getElementById('sample'));
Copied to clipboard
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Syncfusion React Dropdown Tree</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Essential JS 2 for React Components" />
    <meta name="author" content="Syncfusion" />
    <link href="//cdn.syncfusion.com/ej2/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-buttons/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-navigations/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-react-inputs/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-react-dropdowns/styles/material.css" rel="stylesheet" />
	<link href="./styles.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
    <style>
        #loader {
            color: #008cff;
            height: 40px;
            left: 45%;
            position: absolute;
            top: 45%;
            width: 30%;
        }
    </style>
</head>

<body>
    <div id='sample' style="margin: 20px auto 0; width:250px;">
        <div id='loader'>Loading....</div>
    </div>
</body>

</html>

In the following sample, the Dropdown Tree is rendered with custom value of the customTemplate property like Selected items count: 2.

Source
Preview
index.tsx
index.html
Copied to clipboard
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<{}, {}> {
  private data: { [key: string]: Object }[] = [
    { "id": 1, "name": "Steven Buchanan",  "job": "General Manager", "hasChild": true, "expanded": true },
    { "id": 2, "pid": 1, "name": "Laura Callahan",  "job": "Product Manager", "hasChild": true },
    { "id": 3, "pid": 2, "name": "Andrew Fuller",  "job": "Team Lead", "hasChild": true },
    { "id": 4, "pid": 3, "name": "Anne Dodsworth",  "job": "Developer" },
    { "id": 10, "pid": 3, "name": "Lilly",  "job": "Developer", "status":"online" },
    { "id": 5, "pid": 1, "name": "Nancy Davolio",  "job": "Product Manager", "hasChild": true},
    { "id": 6, "pid": 5, "name": "Michael Suyama",  "job": "Team Lead", "hasChild": true },
    { "id": 7, "pid": 6, "name": "Robert King",  "job": "Developer " },
    { "id": 11, "pid": 6, "name": "Mary", "job": "Developer " },
    { "id": 9, "pid": 1, "name": "Janet Leverling",  "job": "HR" }
  ];
  private fields: object = { dataSource: this.data, value: 'id', text: 'name', parentValue: "pid", hasChildren: 'hasChild' };
  private showCheckBox: boolean = true;
  private treeSettings: Object = { autoCheck: true };
  private customTemplate: string = "Selected items count: ${value.length} item(s) ";
  public render() {
    return (<DropDownTreeComponent fields={this.fields} treeSettings={this.treeSettings} customTemplate={this.customTemplate} showCheckBox={this.showCheckBox}mode="Custom" placeholder="Select an employee" popupHeight="200px"/>);
  }
}
ReactDOM.render(<App />, document.getElementById('sample'));
Copied to clipboard
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Syncfusion React Dropdown Tree</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Essential JS 2 for React Components" />
    <meta name="author" content="Syncfusion" />
    <link href="//cdn.syncfusion.com/ej2/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-buttons/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-navigations/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-react-inputs/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-react-dropdowns/styles/material.css" rel="stylesheet" />
	<link href="./styles.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
    <style>
        #loader {
            color: #008cff;
            height: 40px;
            left: 45%;
            position: absolute;
            top: 45%;
            width: 30%;
        }
    </style>
</head>

<body>
    <div id='sample' style="margin: 20px auto 0; width:250px;">
        <div id='loader'>Loading....</div>
    </div>
</body>

</html>