Data binding in Angular DropDownList component
18 Oct 202515 minutes to read
The DropDownList loads data from local data sources or remote data services through the dataSource property. It supports array and DataManager data types.
The DropDownList also supports different kinds of data services, such as OData, OData V4, and Web API, and data formats, such as XML, JSON, and JSONP, with the help of DataManager adapters.
| Fields | Type | Description |
|---|---|---|
| text | string |
Specifies the display text of each list item. |
| value |
number or string
|
Specifies the hidden data value mapped to each list item that should contain a unique value. |
| groupBy | string |
Specifies the category under which the list item has to be grouped. |
| iconCss | string |
Specifies the icon class for each list item. |
| disabled | ||
string |
Defines whether the particular field value is disabled or not. | |
| htmlAttributes | string |
Allows additional attributes such as title, disabled, etc., to configure the elements in various ways to meet the criteria. |
When binding complex data to the DropDownList, ensure that the fields are mapped correctly. Otherwise, the selected item will be undefined.
Binding local data
Local data can be bound to the DropDownList from different types of arrays as described below.
1. Array of simple data
The DropDownList supports loading an array of primitive data, such as strings and numbers. In this case, the value and text fields are mapped to the same field.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import { DropDownListModule } from '@syncfusion/ej2-angular-dropdowns'
import { ButtonModule } from '@syncfusion/ej2-angular-buttons'
import { Component } from '@angular/core';
@Component({
imports: [
FormsModule, ReactiveFormsModule, DropDownListModule, ButtonModule
],
standalone: true,
selector: 'app-root',
// specifies the template string for the DropDownList component with change event
template: `<ejs-dropdownlist id='ddlelement' #samples [dataSource]='data' [placeholder]='placeholder'></ejs-dropdownlist>`
})
export class AppComponent {
constructor() {
}
// defined the array of data
public data: string[] = ['Badminton', 'Basketball', 'Cricket', 'Golf', 'Hockey', 'Rugby'];
// set placeholder text to DropDownList input element
public placeholder: string = 'Select a game';
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));2. Binding to an array of objects
The DropDownList can generate its list items from an array of objects. To do this, map the appropriate data source columns to the fields property.
In the following example, the Id and Game columns are mapped to the value and text fields, respectively.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import { DropDownListModule } from '@syncfusion/ej2-angular-dropdowns'
import { ButtonModule } from '@syncfusion/ej2-angular-buttons'
import { Component } from '@angular/core';
@Component({
imports: [
FormsModule, ReactiveFormsModule, DropDownListModule, ButtonModule
],
standalone: true,
selector: 'app-root',
// specifies the template string for the DropDownList component with change event
template: `<ejs-dropdownlist id='ddlelement' #samples [dataSource]='data' [fields]='fields' [placeholder]='text'></ejs-dropdownlist>`
})
export class AppComponent {
constructor() {
}
// defined the array of data
public data: { [key: string]: Object }[] = [ { Id: 'game1', Game: 'Badminton' },
{ Id: 'game2', Game: 'Football' }, { Id: 'game3', Game: 'Tennis' }];
// maps the appropriate column to fields property
public fields: Object = { text: 'Game', value: 'Id' };
//set the placeholder to DropDownList input
public text: string = "Select a game";
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));3. Binding to an array of complex objects
The DropDownList can also be bound to an array of nested complex objects. To do this, provide the nested column name to the value and text fields.
In the following example, the Code.Id and Country.Name nested columns are mapped to the value and text fields, respectively.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import { DropDownListModule } from '@syncfusion/ej2-angular-dropdowns'
import { ButtonModule } from '@syncfusion/ej2-angular-buttons'
import { Component } from '@angular/core';
@Component({
imports: [
FormsModule, ReactiveFormsModule, DropDownListModule, ButtonModule
],
standalone: true,
selector: 'app-root',
// specifies the template string for the DropDownList component with change event
template: `<ejs-dropdownlist id='ddlelement' #samples [dataSource]='data' [fields]='fields' [placeholder]='text'></ejs-dropdownlist>`
})
export class AppComponent {
constructor() {
}
// defined the array of data
public data: { [key: string]: Object }[] = [
{ Country: { Name: 'Australia' }, Code: { Id: 'AU' }},
{ Country: { Name: 'Bermuda' },Code: { Id: 'BM' }},
{ Country:{ Name: 'Canada'}, Code:{ Id: 'CA'} },
{ Country:{Name: 'Cameroon'}, Code:{ Id: 'CM'} },
{ Country:{Name: 'Denmark'}, Code:{ Id: 'DK' }},
{ Country:{Name: 'France'}, Code: { Id:'FR'} }
];
// maps the appropriate column to fields property
public fields: Object = { text: 'Country.Name', value: 'Code.Id' };
//set the placeholder to DropDownList input
public text: string = "Select a country";
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Binding remote data
The DropDownList supports retrieving data from remote data services with the help of the DataManager component. The Query property is used to fetch data from the database and bind it to the DropDownList.
The following sample displays the first six contacts from the “Customers” table of the Northwind Data Service.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import { DropDownListModule } 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, DropDownListModule, ButtonModule
],
standalone: true,
selector: 'app-root',
// specifies the template string for the DropDownList component with change event
template: `<ejs-dropdownlist id='ddlelement' #samples [dataSource]='data' [fields]='fields' [placeholder]='text' [query]='query' [sortOrder]='sorting'></ejs-dropdownlist>`
})
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: 'ContactName', value: 'CustomerID' };
//bind the Query instance to query property
public query: Query = new Query().from('Customers').select(['ContactName', 'CustomerID']).take(6);
//set the placeholder to DropDownList input
public text: string = "Select a customer";
//sort the result items
public sorting: string = 'Ascending';
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Data binding using Async pipe
An Observable is used extensively by Angular since it provides significant benefits over other techniques for event handling, asynchronous programming, and handling multiple values.
The DropDownList data can be consumed from an Observable object by piping it through an async pipe. The async pipe subscribes to the observable object and resolves the latest value it has emitted.
[app.component.ts]
import { Component, ViewChild } from '@angular/core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-root',
// specifies the template string for the DropDownList component with dataSource
template: `<ejs-dropdownlist id='customers2' formControlName="skillname" name="skillname" #remote2 [dataSource]='data | async' [fields]='remoteFields' [placeholder]='remoteWaterMark' ></ejs-dropdownlist >`,
})
export class AppComponent {
constructor(private http: HttpClient){
this.data=this.http.get<{[key: string]:object;}[]>('https://services.odata.org/V4/Northwind/Northwind.svc/Customers').pipe(
map((results : {[key: string]:any;}) => {
return results['value'];
})
);
}
public data: Observable<any>;
// maps the remote data column to fields property
public remoteFields: Object = { value: 'CustomerID' };
// set the placeholder to DropDownList input element
public remoteWaterMark: string = 'Select a customer';
}[app.module.ts]
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { DropDownListModule, AutoCompleteModule} from '@syncfusion/ej2-angular-dropdowns';
import { AppComponent } from './app.component';
import {DialogModule} from '@syncfusion/ej2-angular-popups';
import { ReactiveFormsModule } from '@angular/forms';
@NgModule({
imports: [
BrowserModule,
FormsModule,
DropDownListModule,
AutoCompleteModule,
DialogModule,
HttpClientModule,
ReactiveFormsModule
],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }[main.ts]
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';
enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);