Introduction
A Combo box control is very commonly used in Web applications where we need to select one item from a list of available items.
Ignite UI library provides a set of rich UI controls including a Combo box, iigCombo, that comes with advanced features such as multi-selection, animation, grouping, style, header, footer and item template. You can learn more about IgCombo control here.
Let's get started.
We are using Visual Studio code as IDE for this example and Angular's latest version. If you do not have IgniteUI setup, start here.
Simple Combo
Let's make a new project in Angular. I am assuming you’ve already set up the NodeJS and IgniteUI for the Angular project. Use the below command to make a new project. Here we are naming this project as IgniteUI-Combo. See listing 1.
- “ng new IgniteUI-Combo”
Listing 1.
Now go to the app.module.ts file and add the reference of ComboModule. See listing 2.
- import { IgxComboModule } from "igniteui-angular";
- // to use ngModel in page
- import { FormsModule } from "@angular/forms";
- // for combo animation
- import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
And in Import section of app.module.ts file add the following line. See listing 2.
- imports : [
- IgxComboModule,
- FormsModule,
- BrowserAnimationsModule
- ]
Listing 2.
Let’s make a new component where we use IgniteUI-Combo. To create a new component, refer to listing 3.
- “ng generate component page1”
- or
- “ng g c page1”
Listing 3.
To call this component from the page, go to the app.component.html and delete all the content and simply write the selector of our page1 component which is ‘app-page1’. See listing 4.
- <app-page1></app-page1>
Listing 4.
Let’s create a data source for our combo box. We will use JSON data to bind the combo box in page1.components.ts. We are using static but you can call the API and bind dynamic data with combo too. Refer to listing 5.
- countriesStaticData = [
- { 'countryId': '1', 'countryName': 'India' },
- { 'countryId': '2', 'countryName': 'China' },
- { 'countryId': '3', 'countryName': 'United States' },
- { 'countryId': '4', 'countryName': 'Australia' },
- { 'countryId': '5', 'countryName': 'United Kingdom' },
- { 'countryId': '6', 'countryName': 'Egypt' }
- ;
Listing 5.
Now go to the page1.component.html page and add igx-combo control on page1.components.html. See listing 6.
- <igx-combo class="input-container" [data]="countriesStaticData" [displayKey]="'countryName'" placeholder="Countries" [valueKey]="'countryId'"> </igx-combo>
Listing 6.
This is a very basic combo and it’ll work like any other combo box but there are some in-built features that are only provided by Ignite UI as follows,
- Filter - To make combo filterable via text all you need to do is add property in igx-combo. i.e. [filterable]="true" and to make placeholder in search box searchPlaceholder="Search...".
- Item Height - We can also set the item height of combo list by add property “itemsMaxHeight”.
- Custom Value - Custom values can also be added at run time. To add this functionality to combo add the “allowCustomValues” property of igx-combo control.
To add custom value we can also invoke “(onAddition) = "handleAddition($event)"” to make custom item permanent.
- Two-way binding - It also support the two-way binding, we can set the combo value on ngOnInit or we can get the value of combo.
For the example, I’ve set the allowCustomValues to true didn't add the correspondent handling event in page1.component.ts file. It will not give any error, in fact it’ll add the custom value to list temporarily. So the final code for page1.component.html will be like listing 7. I have also bound what value of combo is selected in the page.
- <div class="container" style="margin-top: 20px">
- <div class="row">
- <div class="col-sm-3">
- <igx-combo class="input-container" [(ngModel)]="CountryObj" width="270px" [data]="countriesStaticData" [displayKey]="'countryName'"
- [valueKey]="'countryId'" placeholder="Countries" [allowCustomValues]="true" [itemsMaxHeight]="250" [filterable]="true" searchPlaceholder="Search...">
- </igx-combo>
- </div>
- <div class="col-sm-4">
- <span *ngFor="let o of CountryObj">
- CountryId :
- <b>{{o.countryId}}</b> , CountryName :
- <b> {{o.countryName}}</b>
- <br>
- </span>
- </div>
- </div>
- </div>
Listing 7.
To run the app type ng serve --open in terminal and the final output will be like Figure 1.

Figure 1.
Combo With Template
Let’s extend this example more and add another combo for state. In State Combo we will demonstrate the following,
- Filtering the data based on selected countries.
- Grouping the states by countries.
- Custom header, footer and item template.
Let’s add state data for second combo. See listing 8.
- statesStaticData = [
- { 'countryName': 'India', 'countryId': '1', 'stateId': '1', 'stateName': 'Delhi' },
- { 'countryName': 'India', 'countryId': '1', 'stateId': '2', 'stateName': 'Mumbai' },
- { 'countryName': 'India', 'countryId': '1', 'stateId': '3', 'stateName': 'Banglore' },
- { 'countryName': 'China', 'countryId': '2', 'stateId': '4', 'stateName': 'Hunam' },
- { 'countryName': 'China', 'countryId': '2', 'stateId': '5', 'stateName': 'Yunnan' },
- { 'countryName': 'China', 'countryId': '2', 'stateId': '6', 'stateName': 'Jiangsu' },
- { 'countryName': 'United States', 'countryId': '3', 'stateId': '7', 'stateName': 'Texas' },
- { 'countryName': 'United States', 'countryId': '3', 'stateId': '8', 'stateName': 'New York' },
- { 'countryName': 'United States', 'countryId': '3', 'stateId': '9', 'stateName': 'Connecticut' },
- { 'countryName': 'United Kingdom', 'countryId': '4', 'stateId': '10', 'stateName': 'Queensland' },
- { 'countryName': 'United Kingdom', 'countryId': '4', 'stateId': '11', 'stateName': 'Victoria' },
- { 'countryName': 'United Kingdom', 'countryId': '4', 'stateId': '12', 'stateName': 'New South Wales' },
- { 'countryName': 'United Kingdom', 'countryId': '5', 'stateId': '13', 'stateName': 'England' },
- { 'countryName': 'United Kingdom', 'countryId': '5', 'stateId': '14', 'stateName': 'Northern Ireland' },
- { 'countryName': 'United Kingdom', 'countryId': '5', 'stateId': '15', 'stateName': 'Scotland and Wales' },
- { 'countryName': 'Egypt', 'countryId': '6', 'stateId': '16', 'stateName': 'Aswan' },
- { 'countryName': 'Egypt', 'countryId': '6', 'stateId': '17', 'stateName': 'Cairo' },
- { 'countryName': 'Egypt', 'countryId': '6', 'stateId': '18', 'stateName': 'Hurghada' }
- ];





Atul RawatPosted Aug 17, 2018, 1:45 AM
Nice article jatin