Data binding in Angular Dropdown Tree component
4 Sep 202520 minutes to read
The Dropdown Tree component provides flexible data binding capabilities to display hierarchical data from various sources. Data binding is essential for populating the Dropdown Tree with dynamic content and can be configured through the dataSource property, which is a member of the fields property. This enables seamless integration with both local data arrays and remote data services.
The dataSource property supports array of JavaScript objects and DataManager. It also supports different kinds of data services such as OData, OData V4, Web API, URL, and JSON with the help of DataManager adaptors.
The Dropdown Tree supports load on demand (lazy loading) functionality, which optimizes performance by reducing bandwidth consumption when working with large datasets. By default, the loadOnDemand is set to false. When enabled through the treeSettings property, it loads only the first level items initially. Child items are loaded dynamically when their parent item is expanded, based on the parentValue or child field mapping.
Local data
To bind local data to the Dropdown Tree, assign an array of JavaScript objects to the dataSource property.
The Dropdown Tree component requires three essential fields (value, text, and parentValue) to render local data source effectively. When field mappings are not explicitly specified, the component uses default values as the mapping fields. The value field serves as the unique identifier, text displays the node content, and parentValue establishes the hierarchical relationship. Local data source can also be provided as an instance of the DataManager. The component supports two primary local data binding methods.
-
Hierarchical data
-
Self-referential data
Hierarchical data
Dropdown Tree can be populated with hierarchical data source that contains nested arrays of JSON objects. This structure represents parent-child relationships through nested object arrays rather than reference-based relationships. You can directly map the hierarchical data and the field members with corresponding key values from the hierarchical data to the fields property.
In the following example, code, name, and countries columns from the hierarchical data have been mapped to value, text, and child fields, respectively.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import { DropDownTreeModule } from '@syncfusion/ej2-angular-dropdowns'
import { ButtonModule } from '@syncfusion/ej2-angular-buttons'
import { Component } from '@angular/core';
@Component({
imports: [
FormsModule, ReactiveFormsModule, ButtonModule, DropDownTreeModule
],
standalone: true,
selector: 'app-container',
// specifies the template string for the DropDownTree component
template: `<ejs-dropdowntree id='dropdownTree' [fields]='fields'></ejs-dropdowntree>`
})
export class AppComponent {
constructor() {
}
public data?: { [key: string]: Object; }[] = [
{
code: 'AF', name: 'Africa', countries: [
{ code: 'NGA', name: 'Nigeria' },
{ code: 'EGY', name: 'Egypt' },
{ code: 'ZAF', name: 'South Africa' }
]
},
{
code: 'AS', name: 'Asia', expanded: true, countries: [
{ code: 'CHN', name: 'China' },
{ code: 'IND', name: 'India', selected: true },
{ code: 'JPN', name: 'Japan' }
]
},
{
code: 'EU', name: 'Europe', countries: [
{ code: 'DNK', name: 'Denmark' },
{ code: 'FIN', name: 'Finland' },
{ code: 'AUT', name: 'Austria' }
]
},
{
code: 'NA', name: 'North America', countries: [
{ code: 'USA', name: 'United States of America' },
{ code: 'CUB', name: 'Cuba' },
{ code: 'MEX', name: 'Mexico' }
]
},
{
code: 'SA', name: 'South America', countries: [
{ code: 'BRA', name: 'Brazil' },
{ code: 'COL', name: 'Colombia' },
{ code: 'ARG', name: 'Argentina' }
]
},
{
code: 'OC', name: 'Oceania', countries: [
{ code: 'AUS', name: 'Australia' },
{ code: 'NZL', name: 'New Zealand' },
{ code: 'WSM', name: 'Samoa' }
]
},
{
code: 'AN', name: 'Antarctica', countries: [
{ code: 'BVT', name: 'Bouvet Island' },
{ code: 'ATF', name: 'French Southern Lands' }
]
},
];
public fields: Object = { dataSource: this.data, value: 'code', text: 'name', child: 'countries' };
}@import 'node_modules/@syncfusion/ej2-dropdowns/styles/material.css';
@import 'node_modules/@syncfusion/ej2-base/styles/material.css';
@import 'node_modules/@syncfusion/ej2-popups/styles/material.css';
@import 'node_modules/@syncfusion/ej2-inputs/styles/material.css';
@import 'node_modules/@syncfusion/ej2-lists/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-angular-base/styles/material.css';
@import 'node_modules/@syncfusion/ej2-angular-popups/styles/material.css';
@import 'node_modules/@syncfusion/ej2-angular-buttons/styles/material.css';
@import 'node_modules/@syncfusion/ej2-angular-dropdowns/styles/material.css';
#container {
visibility: hidden;
}
#loader {
color: #008cff;
height: 40px;
width: 30%;
position: absolute;
top: 45%;
left: 45%;
}
#wrapper {
width: 250px;
margin: 0 auto;
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Self-referential data
Dropdown Tree can be populated from self-referential data structure that contains an array of JSON objects with parentValue mapping. This structure represents hierarchical relationships through parent-child references within a flat array, where each item contains a reference to its parent item.
You can directly assign the self-referential data and map all the field members with corresponding key values from self-referential data to the fields property.
To render the root level items, specify the parentValue as null or no need to specify the parentValue in the dataSource.
In the following example, id, pid, hasChild, and name columns from self-referential data have been mapped to value, parentValue, hasChildren, and text fields, respectively.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import { DropDownTreeModule } from '@syncfusion/ej2-angular-dropdowns'
import { ButtonModule } from '@syncfusion/ej2-angular-buttons'
import { Component } from '@angular/core';
@Component({
imports: [
FormsModule, ReactiveFormsModule, ButtonModule, DropDownTreeModule
],
standalone: true,
selector: 'app-container',
// specifies the template string for the DropDownTree component
template: `<ejs-dropdowntree id='dropdownTree' [fields]='fields'></ejs-dropdowntree>`
})
export class AppComponent {
constructor() {
}
// dataSource
public data: { [key: string]: Object }[] = [
{ id: 1, name: 'Discover Music', hasChild: true, expanded: true },
{ id: 2, pid: 1, name: 'Hot Singles' },
{ id: 3, pid: 1, name: 'Rising Artists' },
{ id: 4, pid: 1, name: 'Live Music' },
{ id: 6, pid: 1, name: 'Best of 2017 So Far' },
{ id: 7, name: 'Sales and Events', hasChild: true },
{ id: 8, pid: 7, name: '100 Albums - $5 Each' },
{ id: 9, pid: 7, name: 'Hip-Hop and R&B Sale' },
{ id: 10, pid: 7, name: 'CD Deals' },
{ id: 11, name: 'Categories', hasChild: true },
{ id: 12, pid: 11, name: 'Songs' },
{ id: 13, pid: 11, name: 'Bestselling Albums' },
{ id: 14, pid: 11, name: 'New Releases' },
{ id: 15, pid: 11, name: 'Bestselling Songs' },
{ id: 16, name: 'MP3 Albums', hasChild: true },
{ id: 17, pid: 16, name: 'Rock' },
{ id: 18, pid: 16, name: 'Gospel' },
{ id: 19, pid: 16, name: 'Latin Music' },
{ id: 20, pid: 16, name: 'Jazz' },
{ id: 21, name: 'More in Music', hasChild: true },
{ id: 22, pid: 21, name: 'Music Trade-In' },
{ id: 23, pid: 21, name: 'Redeem a Gift Card' },
{ id: 24, pid: 21, name: 'Band T-Shirts' },
];
// defining fieldMapping
public fields: Object = { dataSource: this.data, value: 'id', text: 'name', parentValue: "pid", hasChildren: 'hasChild' };
}@import 'node_modules/@syncfusion/ej2-dropdowns/styles/material.css';
@import 'node_modules/@syncfusion/ej2-base/styles/material.css';
@import 'node_modules/@syncfusion/ej2-popups/styles/material.css';
@import 'node_modules/@syncfusion/ej2-inputs/styles/material.css';
@import 'node_modules/@syncfusion/ej2-lists/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-angular-base/styles/material.css';
@import 'node_modules/@syncfusion/ej2-angular-popups/styles/material.css';
@import 'node_modules/@syncfusion/ej2-angular-buttons/styles/material.css';
@import 'node_modules/@syncfusion/ej2-angular-dropdowns/styles/material.css';
#container {
visibility: hidden;
}
#loader {
color: #008cff;
height: 40px;
width: 30%;
position: absolute;
top: 45%;
left: 45%;
}
#wrapper {
width: 250px;
margin: 0 auto;
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Remote data
Dropdown Tree can also be populated from remote data services with the help of the DataManager control and Query property. This approach is ideal for working with large datasets or when data needs to be fetched from external sources.
The component supports different kinds of data services such as OData, OData V4, Web API, URL, and JSON with the help of DataManager adaptors.
You can assign service data as an instance of DataManager to the dataSource property. To interact with remote data source, you must provide the endpoint url.
The DataManager that acts as an interface between the service endpoint and the Dropdown Tree requires the following information to interact with service endpoint properly.
-
DataManager->url: Defines the service endpoint to fetch data. -
DataManager->adaptor: Defines the adaptor option. By default, ODataAdaptor is used for remote binding.
Adaptor is responsible for processing response and request from/to the service endpoint. The @syncfusion/ej2-data package provides some predefined adaptors designed to interact with service endpoints. They are:
-
UrlAdaptor: Used to interact with remote services. This is the base adaptor for all remote based adaptors. -
ODataAdaptor: Used to interact with OData endpoints. -
ODataV4Adaptor: Used to interact with OData V4 endpoints. -
WebApiAdaptor: Used to interact with Web API created under OData standards. -
WebMethodAdaptor: Used to interact with web methods.
In the following example, ODataV4Adaptor is used to fetch data from the remote services. The EmployeeID, FirstName, and EmployeeID columns from the Employees table have been mapped to value, text, and hasChildren fields respectively for first level items.
The OrderID, EmployeeID, and ShipName columns from the orders table have been mapped to value, parentValue, and text fields respectively for second level items.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import { DropDownTreeModule } from '@syncfusion/ej2-angular-dropdowns'
import { ButtonModule } from '@syncfusion/ej2-angular-buttons'
import { Component } from '@angular/core';
import { Query, DataManager, ODataV4Adaptor } from '@syncfusion/ej2-data';
@Component({
imports: [
FormsModule, ReactiveFormsModule, ButtonModule, DropDownTreeModule
],
standalone: true,
selector: 'app-container',
// specifies the template string for the DropDownTree component
template: `<ejs-dropdowntree id='dropdownTree' [fields]='fields'></ejs-dropdowntree>`
})
export class AppComponent {
constructor() {
}
// Use data manager to get dropdown tree data from remote source
public data: Object = new DataManager({
url: 'https://services.odata.org/V4/Northwind/Northwind.svc',
adaptor: new ODataV4Adaptor,
crossDomain: true,
});
// Set queries to filter and fetch remote data
public query: Object = new Query().from('Employees').select('EmployeeID,FirstName,Title').take(5);
public query1: Object = new Query().from('Orders').select('OrderID,EmployeeID,ShipName').take(5);
public 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' }
};
}@import 'node_modules/@syncfusion/ej2-dropdowns/styles/material.css';
@import 'node_modules/@syncfusion/ej2-base/styles/material.css';
@import 'node_modules/@syncfusion/ej2-popups/styles/material.css';
@import 'node_modules/@syncfusion/ej2-inputs/styles/material.css';
@import 'node_modules/@syncfusion/ej2-lists/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-angular-base/styles/material.css';
@import 'node_modules/@syncfusion/ej2-angular-popups/styles/material.css';
@import 'node_modules/@syncfusion/ej2-angular-buttons/styles/material.css';
@import 'node_modules/@syncfusion/ej2-angular-dropdowns/styles/material.css';
#container {
visibility: hidden;
}
#loader {
color: #008cff;
height: 40px;
width: 30%;
position: absolute;
top: 45%;
left: 45%;
}
#wrapper {
width: 250px;
margin: 0 auto;
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));