The ComboBox loads the data either from local data sources or
remote data services using the
dataSource property. It supports
the data type of array
or DataManager
.
The ComboBox 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
adaptors.
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 of each list item. |
When binding complex data to the ComboBox, fields should be mapped correctly. Otherwise, the selected item remains undefined.
Local data can be represented in two ways as described below.
The ComboBox has support to load array of primitive data such as strings and numbers. Here, both value and text field act the same.
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
// specifies the template string for the ComboBox component
template: `<ejs-combobox id='comboelement' #samples [dataSource]='data' [placeholder]='text'></ejs-combobox>`
})
export class AppComponent {
constructor() {
}
// defined the array of data
public data: string[] = ['Badminton', 'Basketball', 'Cricket', 'Golf', 'Hockey', 'Rugby'];
// set placeholder text to ComboBox input element
public text: string = 'Select a game';
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { ComboBoxModule } from '@syncfusion/ej2-angular-dropdowns';
import { ButtonModule } from '@syncfusion/ej2-angular-buttons';
/**
* Module
*/
@NgModule({
imports: [
BrowserModule,FormsModule, ReactiveFormsModule, ComboBoxModule,ButtonModule
],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule { }
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';
enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);
The ComboBox can generate its list items through an array of complex data. For this, the appropriate columns should be mapped to the fields property.
In the following example, Id
column and Game
column from complex data have been mapped to the value
field and text
field, respectively.
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
// specifies the template string for the ComboBox component with change event
template: `<ejs-combobox id='comboelement' #samples [dataSource]='data' [fields]='fields' [placeholder]='text'></ejs-combobox>`
})
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 ComboBox input
public text: string = "Select a game";
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { ComboBoxModule } from '@syncfusion/ej2-angular-dropdowns';
import { ButtonModule } from '@syncfusion/ej2-angular-buttons';
/**
* Module
*/
@NgModule({
imports: [
BrowserModule,FormsModule, ReactiveFormsModule, ComboBoxModule,ButtonModule
],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule { }
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';
enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);
The ComboBox can generate its list items through an array of complex data. For this, the appropriate columns should be mapped to the fields property.
In the following example, Code.Id
column and 'Country.Name
column from complex data have been mapped
to the value
field and text
field, respectively.
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
// specifies the template string for the ComboBox component with change event
template: `<ejs-combobox id='comboelement' #samples [dataSource]='data' [fields]='fields' [placeholder]='text'></ejs-combobox>`
})
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 ComboBox input
public text: string = "Select a country";
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { ComboBoxModule } from '@syncfusion/ej2-angular-dropdowns';
import { ButtonModule } from '@syncfusion/ej2-angular-buttons';
/**
* Module
*/
@NgModule({
imports: [
BrowserModule,FormsModule, ReactiveFormsModule, ComboBoxModule,ButtonModule
],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule { }
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';
enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);
The ComboBox supports retrieval of data from remote data services with the help
of DataManager
component. The Query
property allows is
used to fetch data from the database and bind it to the ComboBox.
In the following sample, displayed first 6 contacts from customer
table of Northwind
Data Service.
import { Component } from '@angular/core';
import { Query, DataManager, ODataV4Adaptor } from '@syncfusion/ej2-data'
@Component({
selector: 'app-root',
// specifies the template string for the ComboBox component with change event
template: `<ejs-combobox id='comboelement' #samples [dataSource]='data' [fields]='fields' [placeholder]='text' [query]='query' [sortOrder]='sorting'></ejs-combobox>`
})
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 ComboBox input
public text: string = "Select a customer";
//sort the result items
public sorting: string = 'Ascending';
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { ComboBoxModule } from '@syncfusion/ej2-angular-dropdowns';
import { ButtonModule } from '@syncfusion/ej2-angular-buttons';
/**
* Module
*/
@NgModule({
imports: [
BrowserModule,FormsModule, ReactiveFormsModule, ComboBoxModule,ButtonModule
],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule { }
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';
enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);
An Observable
is used extensively by Angular since it provide significant benefits over techniques for event handling, asynchronous programming, and handling multiple values.
ComboBox data can be consumed from an Observable
object by piping it through an async
pipe. The async
pipe is used to subscribe the observable object and resolve with the latest value emitted by it.
import { Component } from '@angular/core';
import { Observable } from 'rxjs';
import { CategoriesService } from './categories.service';
import { map } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-root',
// specifies the template string for the ComboBox component with dataSource
template: ` <ejs-combobox id='customers2' formControlName="skillname" name="skillname" #remote2 [dataSource]='data | async' [fields]='remoteFields' [placeholder]='remoteWaterMark' ></ejs-combobox >`,
providers: [CategoriesService]
})
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]:object;}) => {
return results.value;
})
);
}
public data: Observable<any>;
// maps the remote data column to fields property
public remoteFields: Object = { value: 'CustomerID' };
// set the placeholder to ComboBox input element
public remoteWaterMark: string = 'Select a customer';
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { DropDownListModule, ComboBoxModule} from '@syncfusion/ej2-angular-dropdowns';
import { AppComponent } from './app.component';
import { HelloComponent } from './hello.component';
import {DialogModule} from '@syncfusion/ej2-angular-popups';
import { HttpModule, JsonpModule } from '@angular/http';
import { ReactiveFormsModule } from '@angular/forms';
@NgModule({
imports: [
BrowserModule,
FormsModule,
DropDownListModule,
ComboBoxModule,
DialogModule,
HttpClientModule,
ReactiveFormsModule, HttpModule, JsonpModule
],
declarations: [ AppComponent, HelloComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';
enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);