Search results

Getting Started

This section explains how to create a simple ComboBox component and configure its available functionalities in Angular.

Dependencies

The following list of dependencies are required to use the ComboBox component in your application.

|-- @syncfusion/ej2-ng-dropdowns
    |-- @syncfusion/ej2-base
    |-- @syncfusion/ej2-data
    |-- @syncfusion/ej2-ng-base
    |-- @syncfusion/ej2-dropdowns
        |-- @syncfusion/ej2-lists
        |-- @syncfusion/ej2-inputs
        |-- @syncfusion/ej2-popups
            |-- @syncfusion/ej2-buttons

Installation and configuration

  • To setup basic Angular sample use following commands.
git clone https://github.com/angular/quickstart.git quickstart
cd quickstart
npm install

For more information, refer to Angular sample setup.

  • Install Syncfusion packages using below command.
npm install @syncfusion/ej2-ng-dropdowns --save

The above package installs ComboBox dependencies which are required to render the component in the Angular environment.

  • Syncfusion ej2-ng-dropdowns packages needs to be mapped in systemjs.config.js configuration file.
/**
 * System configuration for Angular samples
 * Adjust as necessary for your application needs.
 */
(function (global) {
System.config({
    paths: {
        'npm:': 'node_modules/',
         "syncfusion:": "node_modules/@syncfusion/", //** syncfusion alias **
    },
    map: {

        // our app is within the app folder
        app: 'app',

        // angular bundles
        '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
        '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
        '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
        '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
        '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
        '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
        '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
        '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',

        // syncfusion bundles
        '@syncfusion/ej2-base': 'syncfusion:ej2-base/dist/ej2-base.umd.min.js',
        '@syncfusion/ej2-data': 'syncfusion:ej2-data/dist/ej2-data.umd.min.js',
        '@syncfusion/ej2-inputs': 'syncfusion:ej2-inputs/dist/ej2-inputs.umd.min.js',
        '@syncfusion/ej2-popups': 'syncfusion:ej2-popups/dist/ej2-popups.umd.min.js',
        "@syncfusion/ej2-dropdowns": 'syncfusion:ej2-dropdowns/dist/ej2-dropdowns.umd.min.js',
        '@syncfusion/ej2-ng-base': 'syncfusion:ej2-ng-base/dist/ej2-ng-base.umd.min.js',
        '@syncfusion/ej2-ng-dropdowns': 'syncfusion:ej2-ng-dropdowns/dist/ej2-ng-dropdowns.umd.min.js'

        // other libraries
        'rxjs':                      'npm:rxjs',
        'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js'
    },

    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
      app: {
        defaultExtension: 'js',
        meta: {
          './*.js': {
            loader: 'systemjs-angular-loader.js'
          }
        }
      },
      rxjs: {
        defaultExtension: 'js'
      }
    }
  });
})(this);
  • Import ComboBox module into Angular application(app.module.ts) from the package @syncfusion/ej2-ng-dropdowns.
import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
// import the ComboBoxModule for the ComboBox component
import { ComboBoxModule } from '@syncfusion/ej2-ng-dropdowns';
import { AppComponent }  from './app.component';

@NgModule({
  //declaration of ej2-ng-dropdowns module into NgModule
  imports:      [ BrowserModule, ComboBoxModule ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }
  • Modify the template in app.component.ts file to render the ejs-combobox component and also include the component theme from the corresponding base package @syncfusion/ej2-dropdowns component through styleUrls.
import { Component, ViewEncapsulation } from '@angular/core';

@Component({
  selector: 'my-app',
  // specifies the template string for the ComboBox component
  template: `<ejs-combobox id='comboelement'></ejs-combobox>`,
  // include the material theme to ComboBox
  styleUrls: ['../../node_modules/@syncfusion/ej2-dropdowns/styles/material.css']
  encapsulation: ViewEncapsulation.None
})
export class AppComponent  { }

Binding data source

After initializing, populate the ComboBox with data using the dataSource property. Here, an array of string values passed to ComboBox component.

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

@Component({
    selector: 'app-container',
    // specifies the template string for the ComboBox component
    template: `<ejs-combobox id='comboelement' [dataSource]='data'></ejs-combobox>`
})
export class AppComponent {
    constructor() {
    }
    // defined the array of data
    public data: string[] = ['Cricket', 'Football', 'Rugby', 'Snooker', 'Tennis'];
}

Run the application

After completing the configuration required to render a basic ComboBox, run the following command to display the output in your default browser.

npm start

The following example illustrates the output in your browser.

Source
Preview
app.component.ts
app.module.ts
main.ts
import { Component } from '@angular/core';
import { ComboBoxComponent } from '@syncfusion/ej2-ng-dropdowns';

@Component({
    selector: 'app-container',
    // specifies the template string for the ComboBox component with dataSource
    template: `<ejs-combobox id='comboelement' [dataSource]='data' placeholder = 'Select a game'></ejs-combobox>`
})
export class AppComponent {
    constructor() {
    }
    // defined the array of data
    public data: string[] = ['Cricket', 'Football', 'Rugby', 'Snooker', 'Tennis'];
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { ComboBoxComponent } from '@syncfusion/ej2-angular-dropdowns';
import { ButtonComponent } from '@syncfusion/ej2-angular-buttons';
/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,FormsModule, ReactiveFormsModule
    ],
    declarations: [AppComponent, ComboBoxComponent, ButtonComponent],
    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);

Custom values

The ComboBox allows the user to give input as custom value which is not required to present in predefined set of values. By default, this support is enabled by allowCustom property. In this case, both text field and value field considered as same. The custom value will be sent to post back handler when a form is about to be submitted.

Source
Preview
app.component.ts
app.module.ts
main.ts
import { Component } from '@angular/core';
import { ComboBoxComponent } from '@syncfusion/ej2-ng-dropdowns';

@Component({
    selector: 'app-container',
    // specifies the template string for the ComboBox component with dataSource
    template: `<ejs-combobox id='comboelement' [dataSource]='sportsData' [fields]=fields allowCustom=true placeholder = 'Select a game'></ejs-combobox>`
})
export class AppComponent {
    constructor() {
    }
    public fields: Object = {text: 'Game', value: 'Id'};
    // defined the array of data
    public sportsData: { [key: string]: Object }[] = [
        { Id: 'game1', Game: 'Badminton' },
        { Id: 'game2', Game: 'Football' },
        { Id: 'game3', Game: 'Tennis' }
    ];
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { ComboBoxComponent } from '@syncfusion/ej2-angular-dropdowns';
import { ButtonComponent } from '@syncfusion/ej2-angular-buttons';
/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,FormsModule, ReactiveFormsModule
    ],
    declarations: [AppComponent, ComboBoxComponent, ButtonComponent],
    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);

Configure the popup list

By default, the width of the popup list automatically adjusts according to the ComboBox input element’s width, and the height of the popup list has ‘300px’.

The height and width of the popup list can also be customized using the popupHeight  and popupWidth property respectively.

In the following sample, popup list’s width and height are configured.

Source
Preview
app.component.ts
app.module.ts
main.ts
import { Component, ViewChild } from '@angular/core';
import { ComboBoxComponent } from '@syncfusion/ej2-ng-dropdowns';

@Component({
    selector: 'app-container',
    // specifies the template string for the ComboBox component with value property
    template: `<ejs-combobox id='comboelement' #samples [dataSource]='data' placeholder='Select a game' popupHeight='200px' popupWidth='250px'></ejs-combobox>`
})
export class AppComponent {
    @ViewChild('samples')
    public sportsObj: ComboBoxComponent;
    constructor() {
    }
    // define the array of data
    public data: string[] = ['Cricket', 'Football', 'Golf', 'Rugby', 'Snooker', 'Tennis'];
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { ComboBoxComponent } from '@syncfusion/ej2-angular-dropdowns';
import { ButtonComponent } from '@syncfusion/ej2-angular-buttons';
/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,FormsModule, ReactiveFormsModule
    ],
    declarations: [AppComponent, ComboBoxComponent, ButtonComponent],
    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);

Two-way binding

In ComboBox, the value property supports two-way binding functionality. The following example demonstrates how to work the two-way binding functionality in ComboBox.

Source
Preview
app.component.ts
app.module.ts
main.ts
import { Component } from '@angular/core';

@Component({
    selector: 'app-container',
    // specifies the template string for the ComboBox component and
    // input element for checking the two-way binding support using value property
    template: `
    <ejs-combobox id='comboelement' [dataSource]='data' [(value)]='value' placeholder="Select a game"></ejs-combobox>
    <div style='margin-top: 50px'>
        <input type="text" [(ngModel)]="value" style='width:245px;height:25px' />
     </div>
    `
})
export class AppComponent {
    constructor() {
    }
    // defined the array of complex data
    public data: string[] = [ 'Badminton', 'Football', 'Rugby', 'Snooker', 'Tennis' ];
    // set a value to pre-select
    public value: string = 'Badminton';
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { ComboBoxComponent } from '@syncfusion/ej2-angular-dropdowns';
import { ButtonComponent } from '@syncfusion/ej2-angular-buttons';
/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,FormsModule, ReactiveFormsModule
    ],
    declarations: [AppComponent, ComboBoxComponent, ButtonComponent],
    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);