Filtering in Angular AutoComplete
31 Aug 202615 minutes to read
The AutoComplete component has built-in support for filtering data items. The filter operation starts as soon as you type in the component.
Change the filter type
Determines the filter type the component should use during the search action. The available filterType values and their supported data types are:
| Filter Type | Description | Supported Types |
|---|---|---|
| StartsWith | Checks whether a value begins with the specified value. | String |
| EndsWith | Checks whether a value ends with the specified value. | String |
| Contains | Checks whether a value contains the specified value. | String |
The following example filters the data with the StartsWith type.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import { AutoCompleteModule } from '@syncfusion/ej2-angular-dropdowns'
import { ButtonModule } from '@syncfusion/ej2-angular-buttons'
import { Component } from '@angular/core';
//import DataManager related classes
import { Query, DataManager, ODataV4Adaptor } from '@syncfusion/ej2-data';
@Component({
imports: [
FormsModule, ReactiveFormsModule, AutoCompleteModule, ButtonModule
],
standalone: true,
selector: 'app-root',
// specifies the template string for the AutoComplete component
template: `<ejs-autocomplete id='atcelement' [dataSource]='sportsData' [fields]='fields' [placeholder]='text' [query]='query' [filterType]='filterType' [sortOrder]='sorting'></ejs-autocomplete>`
})
export class AppComponent {
constructor() {
}
//bind the DataManager instance to dataSource property
public sportsData: DataManager = new DataManager({
url: 'https://services.odata.org/V4/Northwind/Northwind.svc/Customers',
adaptor: new ODataV4Adaptor,
crossDomain: true
});
// maps the appropriate column to fields property
public fields: Object = { value: 'ContactName' };
//bind the Query instance to query property
public query: Query = new Query().select(['ContactName', 'CustomerID']).take(6);
//set the placeholder to AutoComplete input
public text: string = "Find a customer";
//set the filterType to searching operation
public filterType: string='StartsWith';
//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));Filter item count
Specify the number of suggestion items to display using the suggestionCount property of the AutoComplete.
The following example restricts the suggestion list to 5 items.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import { AutoCompleteModule } from '@syncfusion/ej2-angular-dropdowns'
import { ButtonModule } from '@syncfusion/ej2-angular-buttons'
import { Component } from '@angular/core';
//import DataManager related classes
import { Query, DataManager, ODataV4Adaptor } from '@syncfusion/ej2-data';
@Component({
imports: [
FormsModule, ReactiveFormsModule, AutoCompleteModule, ButtonModule
],
standalone: true,
selector: 'app-root',
// specifies the template string for the AutoComplete component
template: `<ejs-autocomplete id='atcelement' [dataSource]='sportsData' [fields]='fields' [placeholder]='text' [query]='query' [filterType]='filterType' [sortOrder]='sorting' [suggestionCount]='suggestionCount'></ejs-autocomplete>`
})
export class AppComponent {
constructor() {
}
//bind the DataManager instance to dataSource property
public sportsData: DataManager = new DataManager({
url: 'https://services.odata.org/V4/Northwind/Northwind.svc/Customers',
adaptor: new ODataV4Adaptor,
crossDomain: true
});
// maps the appropriate column to fields property
public fields: Object = { value: 'ContactName' };
//bind the Query instance to query property
public query: Query = new Query().select(['ContactName', 'CustomerID']);
//set the placeholder to AutoComplete input
public text: string = "Find a customer";
//set the filterType to searching operation
public filterType: string='StartsWith';
//sort the result items
public sorting: string = 'Ascending';
//set the suggestionCount to show the maximum suggestion list item
public suggestionCount: Number= 5;
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Limit the minimum filter character
You can set the minimum number of characters required to filter the data on the AutoComplete. This is done by setting the minLength property on the AutoComplete.
In the following example, the remote request does not fetch search data until the search key contains three characters.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import { AutoCompleteModule } from '@syncfusion/ej2-angular-dropdowns'
import { ButtonModule } from '@syncfusion/ej2-angular-buttons'
import { Component } from '@angular/core';
//import DataManager related classes
import { Query, DataManager, ODataV4Adaptor } from '@syncfusion/ej2-data';
@Component({
imports: [
FormsModule, ReactiveFormsModule, AutoCompleteModule, ButtonModule
],
standalone: true,
selector: 'app-root',
// specifies the template string for the AutoComplete component
template: `<ejs-autocomplete id='atcelement' [dataSource]='sportsData' [fields]='fields' [placeholder]='text' [query]='query' [filterType]='filterType' [sortOrder]='sorting' [minLength]='minLength'></ejs-autocomplete>`
})
export class AppComponent {
constructor() {
}
//bind the DataManager instance to dataSource property
public sportsData: DataManager = new DataManager({
url: 'https://services.odata.org/V4/Northwind/Northwind.svc/Customers',
adaptor: new ODataV4Adaptor,
crossDomain: true
});
// maps the appropriate column to fields property
public fields: Object = { value: 'ContactName' };
//bind the Query instance to query property
public query: Query = new Query().select(['ContactName', 'CustomerID']);
//set the placeholder to AutoComplete input
public text: string = "Find a customer";
//set the filterType to searching operation
public filterType: string='StartsWith';
//sort the result items
public sorting: string = 'Ascending';
//set the minLength to restrict the remote request until search key contains 3 characters.
public minLength: Number = 3;
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Case sensitive filtering
Data items can be filtered with or without case sensitivity by setting the ignoreCase property of the AutoComplete.
The following sample shows how to filter the data with case sensitivity.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import { AutoCompleteModule } from '@syncfusion/ej2-angular-dropdowns'
import { ButtonModule } from '@syncfusion/ej2-angular-buttons'
import { Component } from '@angular/core';
@Component({
imports: [
FormsModule, ReactiveFormsModule, AutoCompleteModule, ButtonModule
],
standalone: true,
selector: 'app-root',
// specifies the template string for the AutoComplete component
template: `<ejs-autocomplete id='atcelement' [dataSource]='data' [placeholder]='text' [filterType]='filterType' [sortOrder]='sorting' [ignoreCase]='ignoreCase'></ejs-autocomplete>`
})
export class AppComponent {
constructor() {
}
//bind the DataManager instance to dataSource property
public data: string[] = ['Badminton', 'Basketball', 'Cricket', 'Football', 'Golf', 'Gymnastics', 'Hockey', 'Tennis'];
//set the placeholder to AutoComplete input
public text: string = "Find a game Eg: FootBall";
//set the filterType to searching operation
public filterType: string='StartsWith';
//sort the result items
public sorting: string = 'Ascending';
//set the minLength to restrict the remote request until search key contains 3 characters.
public ignoreCase: Boolean = false;
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Diacritics Filtering
The AutoComplete component supports diacritics filtering, which ignores the diacritics and makes it easier to filter results in lists that contain international characters when the ignoreAccent property is enabled.
In the following sample, data with diacritics is bound as the dataSource for the AutoComplete.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import { AutoCompleteModule } from '@syncfusion/ej2-angular-dropdowns'
import { ButtonModule } from '@syncfusion/ej2-angular-buttons'
import { Component } from '@angular/core';
@Component({
imports: [
FormsModule, ReactiveFormsModule, AutoCompleteModule, ButtonModule
],
standalone: true,
selector: 'app-root',
// specifies the template string for the DropDownList component with change event
template: ` <ejs-autocomplete id='diacritics' [dataSource]='data' [ignoreAccent]='true' placeholder='e.g. aero'>
</ejs-autocomplete>`
})
export class AppComponent {
constructor() {
}
// create local data
public data: string[] = [
'Aeróbics',
'Aeróbics en Agua',
'Aerografía',
'Aeromodelaje',
'Águilas',
'Ajedrez',
'Ala Delta',
'Álbumes de Música',
'Alusivos',
'Análisis de Escritura a Mano'];
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Debounce delay
Use the debounceDelay property for filtering to set a delay, in milliseconds. This reduces the frequency of filtering as you type, enhancing performance and responsiveness for a smoother user experience. By default, a debounceDelay of 300ms is set. To disable this feature entirely, set the value to 0ms.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import { AutoCompleteModule } from '@syncfusion/ej2-angular-dropdowns'
import { ButtonModule } from '@syncfusion/ej2-angular-buttons'
import { Component } from '@angular/core';
@Component({
imports: [
FormsModule, ReactiveFormsModule, AutoCompleteModule, ButtonModule
],
standalone: true,
selector: 'app-root',
// specifies the template string for the AutoComplete component
template: `<ejs-autocomplete id='atcelement' [dataSource]='sportsData' [placeholder]='placeholder' [debounceDelay]='debounceDelay'></ejs-autocomplete>`
})
export class AppComponent {
constructor() {
}
// defined the array of data
public sportsData: string[] = ['Badminton', 'Basketball', 'Cricket', 'Football', 'Golf', 'Gymnastics', 'Hockey', 'Tennis'];
// set placeholder text to AutoComplete input element
public placeholder: string = 'Find a game';
//set the debounceDelay
public debounceDelay:string ='300';
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));