Templates in Angular Drop down list component

27 Sep 202324 minutes to read

The DropDownList has been provided with several options to customize each list items, group title, selected value, header, and footer elements.

To get started quickly with templates in angular DropDownList component, you can check the video below.

Item template

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

In the following sample, each list item is split into two columns to display relevant data’s.

import { Component } from '@angular/core';
import { Query, DataManager, ODataV4Adaptor } from '@syncfusion/ej2-data'

@Component({
    selector: 'app-root',
    // specifies the template url path
    templateUrl: 'template.html'
})
export class AppComponent {
height: any;
    constructor() {
    }
    //bind the DataManager instance to dataSource property
    public data: DataManager = new DataManager({
            url: 'https://services.odata.org/V4/Northwind/Northwind.svc/',
            adaptor: new ODataV4Adaptor,
            crossDomain: true
        });
    // maps the appropriate column to fields property
    public fields: Object = { text: 'FirstName', value: 'EmployeeID' };
    //bind the Query instance to query property
    public query: Query = new Query().from('Employees').select(['FirstName', 'City','EmployeeID']).take(6);
    //set the placeholder to DropDownList input
    public text: string = "Select an employee";
    //sort the result items
    public sorting: string = 'Ascending';
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { DropDownListModule } from '@syncfusion/ej2-angular-dropdowns';
/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,FormsModule,DropDownListModule
    ],
    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);
<div id="wrapper" style='margin-top: 20px'>
	  <div id='content' style="margin: 0px auto; width:300px;">
        <!-- specifies the template string for the DropDownList component-->
        <ejs-dropdownlist id='dropdownlist-template' [dataSource]='data' [fields]='fields' [sortOrder]='sorting' [query]='query' [popupHeight]='height' [placeholder]='text' [itemTemplate]='itemTemplate'>
            <ng-template #itemTemplate="" let-data="">
                <!--set the value to itemTemplate property-->
                <span><span class='name'> </span><span class ='city'></span></span>
            </ng-template>
        </ejs-dropdownlist>
      </div>
    </div>

Value template

The currently selected value that is displayed by default on the DropDownList input element can be customized using the valueTemplate property.

In the following sample, the selected value is displayed as a combined text of both FirstName and Cityin the DropDownList input, which is separated by a hyphen.

import { Component } from '@angular/core';
import { Query, DataManager, ODataV4Adaptor } from '@syncfusion/ej2-data'

@Component({
    selector: 'app-root',
    // specifies the template url path
    templateUrl: 'template.html'
})
export class AppComponent {
    constructor() {
    }
    //bind the DataManager instance to dataSource property
    public data: DataManager = new DataManager({
            url: 'https://services.odata.org/V4/Northwind/Northwind.svc/',
            adaptor: new ODataV4Adaptor,
            crossDomain: true
        });
    // maps the appropriate column to fields property
    public fields: Object = { text: 'FirstName', value: 'EmployeeID' };
    //bind the Query instance to query property
    public query: Query = new Query().from('Employees').select(['FirstName', 'City','EmployeeID']).take(6);
    //set the placeholder to DropDownList input
    public text: string = "Select an employee";
    //sort the result items
    public sorting: string = 'Ascending';
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { DropDownListModule } from '@syncfusion/ej2-angular-dropdowns';
/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,FormsModule,DropDownListModule
    ],
    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);
<div id="wrapper">
	  <div id='content' style="margin: 0px auto; width:300px;">
        <!-- specifies the template string for the DropDownList component-->
        <ejs-dropdownlist id='ddlelement' #samples [dataSource]='data' [fields]='fields' [sortOrder]='sorting' [placeholder]='text' [query]='query' [itemTemplate]='itemTemplate' [valueTemplate]='valueTemplate'>
            <ng-template #itemTemplate="" let-data="">
                <!--set the value to itemTemplate property-->
                <span><span class='name'> </span><span class ='city'></span></span>
            </ng-template>
            <ng-template #valueTemplate="" let-data="">
                <!--set the value to valueTemplate property-->
                <span class='value'> - </span>
            </ng-template>
        </ejs-dropdownlist>
      </div>
    </div>

Group template

The group header title under which appropriate sub-items are categorized can also be customize with the help of groupTemplate property. This template is common for both inline and floating group header template.

In the following sample, employees are grouped according to their city.

import { Component } from '@angular/core';
import { Query, Predicate, DataManager, ODataV4Adaptor } from '@syncfusion/ej2-data'

@Component({
    selector: 'app-root',
    // specifies the template url path
    templateUrl: 'template.html'
})
export class AppComponent {
    constructor() {
    }
    //bind the DataManager instance to dataSource property
    public data: DataManager = new DataManager({
            url: 'https://services.odata.org/V4/Northwind/Northwind.svc/',
            adaptor: new ODataV4Adaptor,
            crossDomain: true
        });
    // form  predicate to fetch the grouped data
    public groupPredicate = new Predicate('City', 'equal','london').or('City','equal','seattle');
    // maps the appropriate column to fields property
    public fields: Object = { text: 'FirstName', value: 'EmployeeID', groupBy:'City' };
    //bind the Query instance to query property
    public query: Query = new Query().from('Employees').select(['FirstName', 'City','EmployeeID']).take(6).where(this.groupPredicate);
    //set the placeholder to DropDownList input
    public text: string = "Select an employee";
    //sort the result items
    public sorting: string = 'Ascending';
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { DropDownListModule } from '@syncfusion/ej2-angular-dropdowns';
/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,FormsModule,DropDownListModule
    ],
    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);
<div id="wrapper" style='margin-top: 20px'>
	  <div id='content' style="margin: 0px auto; width:250px;">
        <!-- specifies the template string for the DropDownList component-->
        <ejs-dropdownlist id='ddlelement' #samples [dataSource]='data' [fields]='fields' [sortOrder]='sorting' [placeholder]='text' [groupTemplate]='groupTemplate' [query]='query'>
            <ng-template #groupTemplate="" let-data="">
                <!--set the value to groupTemplate property-->
                <strong></strong>
            </ng-template>
        </ejs-dropdownlist>
      </div>
    </div>

Header template

The header element is shown statically at the top of the popup list items within the
DropDownList, and any custom element can be placed as a header element using the
headerTemplate property.

In the following sample, the list items and its headers are designed and displayed as two columns similar to multiple columns of the grid.

import { Component } from '@angular/core';
import { Query, DataManager, ODataV4Adaptor } from '@syncfusion/ej2-data';

@Component({
    selector: 'app-root',
    // specifies the template url path
    templateUrl: 'template.html'
})
export class AppComponent {
    constructor() {
    }
    //bind the DataManager instance to dataSource property
    public data: DataManager = new DataManager({
            url: 'https://services.odata.org/V4/Northwind/Northwind.svc/',
            adaptor: new ODataV4Adaptor,
            crossDomain: true
        });
    // maps the appropriate column to fields property
    public fields: Object = { text: 'FirstName', value: 'EmployeeID' };
    //bind the Query instance to query property
    public query: Query = new Query().from('Employees').select(['FirstName', 'City','EmployeeID']).take(6);
    //set the placeholder to DropDownList input
    public text: string = "Select an employee";
    //sort the result items
    public sorting: string = 'Ascending';
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { DropDownListModule } from '@syncfusion/ej2-angular-dropdowns';
/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,FormsModule,DropDownListModule
    ],
    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);
<div id="wrapper" style='margin-top: 20px'>
	  <div id='content' style="margin: 0px auto; width:250px;">
        <!-- specifies the template string for the DropDownList component-->
        <ejs-dropdownlist id='ddlelement' #samples [dataSource]='data' [fields]='fields' [sortOrder]='sorting' [placeholder]='text' [query]='query' [headerTemplate]='headerTemplate' [itemTemplate]='itemTemplate'>
            <ng-template #itemTemplate="" let-data="">
                <!--set the value to itemTemplate property-->
                <span class='item'><span class='name'> </span><span class ='city'></span></span>
            </ng-template>
            <ng-template #headerTemplate="" let-data="">
                <!--set the value to headerTemplate property-->
                <span class='head'><span class='name'>Name</span><span class='city'>City</span></span>
            </ng-template>
        </ejs-dropdownlist>
      </div>
    </div>

The DropDownList 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, footer element displays the total number of list items present in the DropDownList

import { Component } from '@angular/core';

@Component({
    selector: 'app-root',
    // specifies the template url path
    templateUrl: 'template.html'
})
export class AppComponent {
    constructor() {
    }
    // defined the array of data
    public data: Object[] = ['Badminton', 'Basketball', 'Cricket', 'Golf', 'Hockey'];
    // set placeholder text to DropDownList input element
    public text: string = 'Select a game';
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { DropDownListModule } from '@syncfusion/ej2-angular-dropdowns';
/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,FormsModule,DropDownListModule
    ],
    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);
<div id="wrapper" style='margin-top: 20px'>
	  <div id='content' style="margin: 0px auto; width:250px;">
        <!-- specifies the template string for the DropDownList component-->
        <ejs-dropdownlist id='ddlelement' #samples [dataSource]='data' [placeholder]='text' [footerTemplate]='footerTemplate'>
            <ng-template #footerTemplate="" let-data="">
                <!--set the value to footerTemplate property-->
                <span class='foot'> Total list item: 5</span>
            </ng-template>
        </ejs-dropdownlist>
      </div>
    </div>

No records template

The DropDownList is provided with support to custom design the popup list content when no data is found and no matches found on search with the help of
noRecordsTemplate property.

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

import { Component } from '@angular/core';

@Component({
    selector: 'app-root',
    // specifies the template string for the DropDownList component
    template:  `<ejs-dropdownlist id='ddlelement' [dataSource]='data' placeholder='Find a item'>
                    <ng-template #noRecordsTemplate>
                        <span class='norecord'> NO DATA AVAILABLE</span>
                    </ng-template>
                 </ejs-dropdownlist>`
})
export class AppComponent {
    // defined the empty array data
    public data: string[] = [];
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { DropDownListModule } from '@syncfusion/ej2-angular-dropdowns';
/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,FormsModule,DropDownListModule
    ],
    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

There is also an option to custom design the popup list content when the data fetch request fails at the remote server. This can be achieved using the actionFailureTemplate property.

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

import { Component } from '@angular/core';
//import data manager related classes
import { Query, DataManager, ODataV4Adaptor } from '@syncfusion/ej2-data';

@Component({
    selector: 'app-root',
    // specifies the template string for the DropDownList component
    template:  `<ejs-dropdownlist id='ddlelement' [dataSource]='data' [query]='query' [fields]='fields'      placeholder='Find an employee'>
                    <ng-template #actionFailureTemplate>
                        <span class='action-failure'> Data fetch get fails</span>
                    </ng-template>
                 </ejs-dropdownlist>`
})
export class AppComponent {
    //bind the data manager instance to dataSource property
    public data: DataManager = new DataManager({
            // Here, use the wrong url to display the action failure template
            url: 'https://services.odata.org/V4/Northwind/Northwind.svcs/',
            adaptor: new ODataV4Adaptor,
            crossDomain: true
    });
    //bind the Query instance to query property
    public query: Query =  new Query().from('Employees').select(['FirstName']).take(6);

    // maps the appropriate column to fields property
    public fields: Object =  { text: 'FirstName', value: 'EmployeeID' };
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { DropDownListModule } from '@syncfusion/ej2-angular-dropdowns';
/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,FormsModule,DropDownListModule
    ],
    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);

See Also