Combo Component Features
The Angular Combo binds to local data, filters its options as the user types, groups them by a data field, and can be disabled.
The Combo can also load its data from a remote service in chunks, accept custom values, and show its list with custom overlay settings.
Live Demo
Features
Data Binding
The following snippets bind the Angular Combo to a local list of US states through its data property, storing the code of each state and showing its name. The keys that the Combo reads from each item are described in Data Binding in the Combo Overview topic.
import { usStates } from './states-data';
export class ComboDataBindingComponent {
// Items such as { id: 'CT', name: 'Connecticut', region: 'New England' }
public usStates = usStates;
}
<igx-combo [data]="usStates" valueKey="id" displayKey="name"></igx-combo>
Remote Binding
The Combo can load its data from a remote service in chunks: it requests the items the user scrolls to through the dataPreLoad event, and the service returns only those items. The sample below loads products from a remote service while the user scrolls and searches:
Define Remote Service
A remote service loads the items on demand from a server. The Combo exposes its virtualizationState property, which holds the index of the first item to load and the number of items to load. To size the scrollbar for all the items on the server, set the totalItemCount property to their total count.
The following service has a getData() method that builds the request from the state of the Combo and its search text, and emits the loaded items through an observable:
import { HttpClient } from '@angular/common/http';
import { Injectable, inject } from '@angular/core';
import { IForOfState } from 'igniteui-angular/directives';
// import { IForOfState } from '@infragistics/igniteui-angular/directives'; for licensed package
import { BehaviorSubject } from 'rxjs';
@Injectable()
export class RemoteNWindService {
private http = inject(HttpClient);
private url = 'https://services.odata.org/V4/Northwind/Northwind.svc/Products';
public remoteData = new BehaviorSubject<any[]>([]);
public getData(state: IForOfState, searchText: string, cb?: (data: any) => void) {
return this.http.get(this.buildUrl(state, searchText)).subscribe((data: any) => {
this.remoteData.next(data.value);
cb?.(data);
});
}
// Requests the chunk of items the Combo shows, and the total count of the matching items.
private buildUrl(state: IForOfState, searchText: string): string {
let query = `?$skip=${state.startIndex}&$top=${state.chunkSize}&$count=true`;
if (searchText) {
// Double the apostrophes for OData, then encode the text for the URL.
const text = encodeURIComponent(searchText.replace(/'/g, "''"));
query += `&$filter=contains(ProductName, '${text}')`;
}
return `${this.url}${query}`;
}
}
Binding ComboBox to Remote Service
Bind the observable of the service to the Combo with the async pipe, and request a new chunk of items whenever the Combo raises dataPreLoad or the search text changes:
<igx-combo #remoteCombo
[data]="rData | async"
valueKey="ProductID"
displayKey="ProductName"
(dataPreLoad)="onDataPreLoad()"
(searchInputUpdate)="onSearchInputUpdate($event)">
<label igxLabel>Products</label>
</igx-combo>
// The size of the first chunk, requested before the Combo reports how many items it shows.
const FIRST_CHUNK_SIZE = 10;
// The component lists RemoteNWindService in its providers, and the application
// providers include provideHttpClient(), which the service needs.
export class ComboRemoteComponent implements OnInit {
@ViewChild('remoteCombo', { static: true })
public remoteCombo: IgxComboComponent;
private remoteService = inject(RemoteNWindService);
public rData = this.remoteService.remoteData;
private searchText = '';
public ngOnInit() {
this.loadChunk({ startIndex: 0, chunkSize: FIRST_CHUNK_SIZE });
}
// The Combo asks for the next chunk while the user scrolls.
public onDataPreLoad() {
this.loadChunk(this.remoteCombo.virtualizationState);
}
// The server filters the items: request the first chunk of the matching ones.
public onSearchInputUpdate(event: IComboSearchInputEventArgs) {
this.searchText = event.searchText;
this.loadChunk({ startIndex: 0, chunkSize: FIRST_CHUNK_SIZE });
}
private loadChunk(state: IForOfState) {
this.remoteService.getData(state, this.searchText, (data) => {
// The total count sizes the scrollbar for all the items, not only the loaded chunk.
this.remoteCombo.totalItemCount = data['@odata.count'];
});
}
}
Handling Selection
When the Combo is bound to remote data that loads in chunks, and the data items are objects, set the valueKey property. Without it, the Combo compares the selected items with the loaded items by reference, as described in the Combo topic. Every chunk the service loads creates new objects, so the Combo can’t match the selected items with them.
When binding the Combo to remote data, set the valueKey property to a property that is unique to each item.
Custom Overlay Settings
The Combo shows its list of items in an overlay, which you can configure by passing OverlaySettings to the overlaySettings input. The Combo merges these settings over its own defaults, so set only the ones to change. The following settings make the list modal, which dims the page behind it, and close it when the user clicks outside:
import { OverlaySettings } from 'igniteui-angular/core';
// import { OverlaySettings } from '@infragistics/igniteui-angular/core'; for licensed package
export class ComboOverlayComponent {
public overlaySettings: OverlaySettings = {
modal: true,
closeOnOutsideClick: true
};
}
<igx-combo [data]="people" [overlaySettings]="overlaySettings"></igx-combo>
By default, the Combo positions its list with the AutoPositionStrategy, which opens the list below the input, or above it when there isn’t enough room.
Filtering
The Angular Combo filters its list of options as the user types in the search input of the dropdown. Filtering is on by default. Set disableFiltering to turn it off, which also hides the search input.
To let users switch the search between case-sensitive and case-insensitive, set showSearchCaseIcon. The Combo then shows a case-sensitive icon in its search input. The icon has no effect while filtering is disabled, because the search input is hidden.
<!-- Shows the case-sensitive icon in the search input -->
<igx-combo [showSearchCaseIcon]="true"></igx-combo>
<!-- Turns filtering off, which also hides the search input -->
<igx-combo [disableFiltering]="true"></igx-combo>
Filtering Options
The filteringOptions property configures how the Angular Combo matches the search text:
filteringKey- the data field to search in. Defaults to the field set bydisplayKey.caseSensitive- whether the search is case-sensitive. Defaults tofalse. The case-sensitive icon toggles this value.
The following snippet searches the region of each state instead of its name, and makes the search case-sensitive:
import { IComboFilteringOptions } from 'igniteui-angular/combo';
// import { IComboFilteringOptions } from '@infragistics/igniteui-angular/combo'; for licensed package
export class ComboDemo {
public filteringOptions: IComboFilteringOptions = {
filteringKey: 'region',
caseSensitive: true
};
}
<igx-combo [filteringOptions]="filteringOptions"></igx-combo>
Custom Values
The allowCustomValues property lets users add items that are missing from the data. When the search text matches no item, the dropdown shows an Add item button, which adds the search text to the data as a new item.
<igx-combo [allowCustomValues]="true"></igx-combo>
Search Input Focus
When the dropdown of the Angular Combo opens, its search input receives focus.
Set the autoFocusSearch property to false to focus the list of items instead. On mobile devices, this keeps the software keyboard from opening with the dropdown.
<igx-combo [autoFocusSearch]="false"></igx-combo>
Grouping
Set groupKey to a field of the data items to group the options of the Angular Combo under a header for each value of that field:
<igx-combo groupKey="region"></igx-combo>
The groupKey property takes effect only when the data items are objects.
Sorting Direction
The groupSortingDirection property sets the order of the groups in the Combo: SortingDirection.Asc (the default) sorts them in ascending order, SortingDirection.Desc in descending order, and SortingDirection.None keeps them in the order their first items appear in the data. The items inside each group keep their order in the data.
import { SortingDirection } from 'igniteui-angular/core';
// import { SortingDirection } from '@infragistics/igniteui-angular/core'; for licensed package
export class ComboDemo {
public groupSortingDirection = SortingDirection.Desc;
}
<igx-combo groupKey="region" [groupSortingDirection]="groupSortingDirection"></igx-combo>
API References
Additional Resources
Use the following Angular resources to learn more about the Combo: