Templates in Angular Drop down tree component

27 Sep 202324 minutes to read

The Dropdown Tree provides support to customize each list item, header, and footer elements. It uses the Essential JS 2 Template engine to compile and render the elements properly.

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 and {{...}} for ng-template .

import { Component, Inject, OnInit, ViewChild } from '@angular/core';
import { DropDownTreeComponent } from '@syncfusion/ej2-angular-dropdowns';
@Component({
  selector: 'app-container',
  template: `<ejs-dropdowntree id='dropdownTree' cssClass="custom" [fields]='fields' [popupHeight]='height' [placeholder]='watermark' [itemTemplate]='itemTemplate'></ejs-dropdowntree>`
})
export class AppComponent {
  public data: { [key: string]: Object }[] = [
    { id: 1, name: 'Steven Buchanan', eimg: '10', job: 'General Manager', hasChild: true, expanded: true, status:'busy' },
    { id: 2, pid: 1, name: 'Laura Callahan', eimg: '2', job: 'Product Manager', hasChild: true, status:'online' },
    { id: 3, pid: 2, name: 'Andrew Fuller', eimg: '7', job: 'Team Lead', hasChild: true, status:'away' },
    { id: 4, pid: 3, name: 'Anne Dodsworth', eimg: '1', job: 'Developer', status:'busy' },
    { id: 10, pid: 3, name: 'Lilly', eimg: '5', job: 'Developer', status:'online' },
    { id: 5, pid: 1, name: 'Nancy Davolio', eimg: '4', job: 'Product Manager', hasChild: true, status:'away' },
    { id: 6, pid: 5, name: 'Michael Suyama', eimg: '9', job: 'Team Lead', hasChild: true, status:'online' },
    { id: 7, pid: 6, name: 'Robert King', eimg: '8', job: 'Developer ', status:'online' },
    { id: 11, pid: 6, name: 'Mary', eimg: '6', job: 'Developer ', status:'away' },
    { id: 9, pid: 1, name: 'Janet Leverling', eimg: '3', job: 'HR', status:'online' }
  ];
  public fields: Object = { dataSource: this.data, text: 'name', value: 'id', parentValue: 'pid', hasChildren: 'hasChild' };
  public height: string = '300px';
  public watermark: string = 'Select an employee';
  public itemTemplate: string = '<div><span class="ename">${name} - </span><span class="ejob">${job}</span></div>';
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { DropDownTreeModule } from '@syncfusion/ej2-angular-dropdowns';
import { ButtonModule } from '@syncfusion/ej2-angular-buttons';
/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,FormsModule, ReactiveFormsModule, ButtonModule, DropDownTreeModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

import 'zone.js';
enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);

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.

import { Component, Inject, OnInit, ViewChild } from '@angular/core';
import { DropDownTreeComponent } from '@syncfusion/ej2-angular-dropdowns';
@Component({
  selector: 'app-container',
  template: `<ejs-dropdowntree id='dropdownTree' cssClass="custom" [fields]='fields' [popupHeight]='height' [placeholder]='watermark' [headerTemplate]='headerTemplate'></ejs-dropdowntree>`
})
export class AppComponent {
  public data: { [key: string]: Object }[] = [
    { id: 1, name: 'Steven Buchanan', eimg: '10', job: 'General Manager', hasChild: true, expanded: true, status:'busy' },
    { id: 2, pid: 1, name: 'Laura Callahan', eimg: '2', job: 'Product Manager', hasChild: true, status:'online' },
    { id: 3, pid: 2, name: 'Andrew Fuller', eimg: '7', job: 'Team Lead', hasChild: true, status:'away' },
    { id: 4, pid: 3, name: 'Anne Dodsworth', eimg: '1', job: 'Developer', status:'busy' },
    { id: 10, pid: 3, name: 'Lilly', eimg: '5', job: 'Developer', status:'online' },
    { id: 5, pid: 1, name: 'Nancy Davolio', eimg: '4', job: 'Product Manager', hasChild: true, status:'away' },
    { id: 6, pid: 5, name: 'Michael Suyama', eimg: '9', job: 'Team Lead', hasChild: true, status:'online' },
    { id: 7, pid: 6, name: 'Robert King', eimg: '8', job: 'Developer ', status:'online' },
    { id: 11, pid: 6, name: 'Mary', eimg: '6', job: 'Developer ', status:'away' },
    { id: 9, pid: 1, name: 'Janet Leverling', eimg: '3', job: 'HR', status:'online' }
  ];
  public fields: Object = { dataSource: this.data, text: 'name', value: 'id', parentValue: 'pid', hasChildren: 'hasChild' };
  public height: string = '300px';
  public watermark: string = 'Select an employee';
  public headerTemplate: string = '<div class="head"> Employee List </div>';
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { DropDownTreeModule } from '@syncfusion/ej2-angular-dropdowns';
import { ButtonModule } from '@syncfusion/ej2-angular-buttons';
/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,FormsModule, ReactiveFormsModule, ButtonModule, DropDownTreeModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

import 'zone.js';
enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);

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.

import { Component, Inject, OnInit, ViewChild } from '@angular/core';
import { DropDownTreeComponent } from '@syncfusion/ej2-angular-dropdowns';
@Component({
  selector: 'app-container',
  template: `<ejs-dropdowntree id='dropdownTree' cssClass="custom" [fields]='fields' [popupHeight]='height' [placeholder]='watermark' [footerTemplate]='footerTemplate'></ejs-dropdowntree>`
})
export class AppComponent {
  public data: { [key: string]: Object }[] = [
    { id: 1, name: 'Steven Buchanan', eimg: '10', job: 'General Manager', hasChild: true, expanded: true, status:'busy' },
    { id: 2, pid: 1, name: 'Laura Callahan', eimg: '2', job: 'Product Manager', hasChild: true, status:'online' },
    { id: 3, pid: 2, name: 'Andrew Fuller', eimg: '7', job: 'Team Lead', hasChild: true, status:'away' },
    { id: 4, pid: 3, name: 'Anne Dodsworth', eimg: '1', job: 'Developer', status:'busy' },
    { id: 10, pid: 3, name: 'Lilly', eimg: '5', job: 'Developer', status:'online' },
    { id: 5, pid: 1, name: 'Nancy Davolio', eimg: '4', job: 'Product Manager', hasChild: true, status:'away' },
    { id: 6, pid: 5, name: 'Michael Suyama', eimg: '9', job: 'Team Lead', hasChild: true, status:'online' },
    { id: 7, pid: 6, name: 'Robert King', eimg: '8', job: 'Developer ', status:'online' },
    { id: 11, pid: 6, name: 'Mary', eimg: '6', job: 'Developer ', status:'away' },
    { id: 9, pid: 1, name: 'Janet Leverling', eimg: '3', job: 'HR', status:'online' }
  ];
  public fields: Object = { dataSource: this.data, text: 'name', value: 'id', parentValue: 'pid', hasChildren: 'hasChild' };
  public height: string = '300px';
  public watermark: string = 'Select an employee';
  public footerTemplate: string = "<span class='foot'> Total number of employees: "+ this.data.length +"</span>";
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { DropDownTreeModule } from '@syncfusion/ej2-angular-dropdowns';
import { ButtonModule } from '@syncfusion/ej2-angular-buttons';
/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,FormsModule, ReactiveFormsModule, ButtonModule, DropDownTreeModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

import 'zone.js';
enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);

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.

import { Component, Inject, OnInit, ViewChild } from '@angular/core';
import { DropDownTreeComponent } from '@syncfusion/ej2-angular-dropdowns';
@Component({
  selector: 'app-container',
  template: `<ejs-dropdowntree id='dropdownTree' [fields]='fields' [popupHeight]='height' [placeholder]='watermark' [noRecordsTemplate]='noRecordsTemplate'></ejs-dropdowntree>`
})
export class AppComponent {
  public data: { [key: string]: Object }[] = [ ];
  public fields: Object = { dataSource: this.data, text: 'name', value: 'id', parentValue: 'pid', hasChildren: 'hasChild' };
  public height: string = '300px';
  public watermark: string = 'Select an employee';
  public noRecordsTemplate: Object = '<span class="norecord"> NO DATA AVAILABLE</span>';
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { DropDownTreeModule } from '@syncfusion/ej2-angular-dropdowns';
import { ButtonModule } from '@syncfusion/ej2-angular-buttons';
/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,FormsModule, ReactiveFormsModule, ButtonModule, DropDownTreeModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

import 'zone.js';
enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);

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.

import { Component, Inject, OnInit, ViewChild } from '@angular/core';
import { DropDownTreeComponent } from '@syncfusion/ej2-angular-dropdowns';
import { Query, DataManager, ODataV4Adaptor } from '@syncfusion/ej2-data';
@Component({
  selector: 'app-container',
  template: `<ejs-dropdowntree id='dropdownTree' [placeholder]='placeholder' [fields]='field' [actionFailureTemplate]='actionFailureTemplate'></ejs-dropdowntree>`
})
export class AppComponent {
    public data: Object = new DataManager({
      url: 'https://services.odata.org/V4/Northwind/Northwind.svs',
      adaptor: new ODataV4Adaptor,
      crossDomain: true,
    });
    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 field: 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' }
    };
    public actionFailureTemplate: Object = '<span class="action-failure"> Data fetch request fails<span>';
    public placeholder: string ='Select an employee';
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { DropDownTreeModule } from '@syncfusion/ej2-angular-dropdowns';
import { ButtonModule } from '@syncfusion/ej2-angular-buttons';
/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,FormsModule, ReactiveFormsModule, ButtonModule, DropDownTreeModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

import 'zone.js';
enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);

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”.

import { Component, Inject, OnInit, ViewChild } from '@angular/core';
import { DropDownTreeComponent } from '@syncfusion/ej2-angular-dropdowns';
@Component({
  selector: 'app-container',
  template: `<ejs-dropdowntree id='dropdownTree' [popupHeight]='height' [placeholder]='watermark' [showCheckBox]='showCheckBox' [mode]='mode' [customTemplate]='customTemplate' [treeSettings]='treeSettings' [fields]='fields'></ejs-dropdowntree>`
})
export class AppComponent {
  public data: { [key: string]: Object }[] = [
    { id: 1, name: 'Steven Buchanan', eimg: '10', job: 'General Manager', hasChild: true, expanded: true, status:'busy' },
    { id: 2, pid: 1, name: 'Laura Callahan', eimg: '2', job: 'Product Manager', hasChild: true, status:'online' },
    { id: 3, pid: 2, name: 'Andrew Fuller', eimg: '7', job: 'Team Lead', hasChild: true, status:'away' },
    { id: 4, pid: 3, name: 'Anne Dodsworth', eimg: '1', job: 'Developer', status:'busy' },
    { id: 10, pid: 3, name: 'Lilly', eimg: '5', job: 'Developer', status:'online' },
    { id: 5, pid: 1, name: 'Nancy Davolio', eimg: '4', job: 'Product Manager', hasChild: true, status:'away' },
    { id: 6, pid: 5, name: 'Michael Suyama', eimg: '9', job: 'Team Lead', hasChild: true, status:'online' },
    { id: 7, pid: 6, name: 'Robert King', eimg: '8', job: 'Developer ', status:'online' },
    { id: 11, pid: 6, name: 'Mary', eimg: '6', job: 'Developer ', status:'away' },
    { id: 9, pid: 1, name: 'Janet Leverling', eimg: '3', job: 'HR', status:'online' }
  ];
  public fields: Object = { dataSource: this.data, text: 'name', value: 'id', parentValue: 'pid', hasChildren: 'hasChild' };
  public height: string = '300px';
  public watermark: string = 'Select an employee';
  public showCheckBox = true;
  public mode = "Custom";
  public customTemplate = "${value.length} item(s) selected";
  public treeSettings: Object = { autoCheck: true };
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { DropDownTreeModule } from '@syncfusion/ej2-angular-dropdowns';
import { ButtonModule } from '@syncfusion/ej2-angular-buttons';
/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,FormsModule, ReactiveFormsModule, ButtonModule, DropDownTreeModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

import 'zone.js';
enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);

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

import { Component, Inject, OnInit, ViewChild } from '@angular/core';
import { DropDownTreeComponent } from '@syncfusion/ej2-angular-dropdowns';
@Component({
  selector: 'app-container',
  template: `<ejs-dropdowntree id='dropdownTree' [popupHeight]='height' [placeholder]='watermark' [showCheckBox]='showCheckBox' [mode]='mode' [customTemplate]='customTemplate' [treeSettings]='treeSettings' [fields]='fields'></ejs-dropdowntree>`
})
export class AppComponent {
  public data: { [key: string]: Object }[] = [
    { id: 1, name: 'Steven Buchanan', eimg: '10', job: 'General Manager', hasChild: true, expanded: true, status:'busy' },
    { id: 2, pid: 1, name: 'Laura Callahan', eimg: '2', job: 'Product Manager', hasChild: true, status:'online' },
    { id: 3, pid: 2, name: 'Andrew Fuller', eimg: '7', job: 'Team Lead', hasChild: true, status:'away' },
    { id: 4, pid: 3, name: 'Anne Dodsworth', eimg: '1', job: 'Developer', status:'busy' },
    { id: 10, pid: 3, name: 'Lilly', eimg: '5', job: 'Developer', status:'online' },
    { id: 5, pid: 1, name: 'Nancy Davolio', eimg: '4', job: 'Product Manager', hasChild: true, status:'away' },
    { id: 6, pid: 5, name: 'Michael Suyama', eimg: '9', job: 'Team Lead', hasChild: true, status:'online' },
    { id: 7, pid: 6, name: 'Robert King', eimg: '8', job: 'Developer ', status:'online' },
    { id: 11, pid: 6, name: 'Mary', eimg: '6', job: 'Developer ', status:'away' },
    { id: 9, pid: 1, name: 'Janet Leverling', eimg: '3', job: 'HR', status:'online' }
  ];
  public fields: Object = { dataSource: this.data, text: 'name', value: 'id', parentValue: 'pid', hasChildren: 'hasChild' };
  public height: string = '300px';
  public watermark: string = 'Select an employee';
  public showCheckBox = true;
  public mode = "Custom";
  public customTemplate = "Selected item(s) count: ${value.length} ";
  public treeSettings: Object = { autoCheck: true };
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { DropDownTreeModule } from '@syncfusion/ej2-angular-dropdowns';
import { ButtonModule } from '@syncfusion/ej2-angular-buttons';
/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,FormsModule, ReactiveFormsModule, ButtonModule, DropDownTreeModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

import 'zone.js';
enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);