Filtering in EJ2 TypeScript Auto complete control
2 May 202316 minutes to read
The AutoComplete has built-in support to filter data items. The filter operation starts as soon as you start typing characters in the component.
Change the filter type
Determines on which filter type, the component needs to be considered on search action. The available filterType
and its 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 specified value. | String |
Contains | Checks whether a value contains with specified value. | String |
The following examples shows the data filtering is done with StartsWith
type.
import { AutoComplete } from '@syncfusion/ej2-dropdowns';
//import DataManager related classes
import { Query, DataManager, ODataV4Adaptor } from '@syncfusion/ej2-data';
//initiates the component
let filter: AutoComplete = new AutoComplete({
//bind the DataManager instance to dataSource property
dataSource: new DataManager({
url: 'https://services.odata.org/V4/Northwind/Northwind.svc/Customers',
adaptor: new ODataV4Adaptor,
crossDomain: true
}),
//bind the Query instance to query property
query: new Query().select(['ContactName']),
//map the appropriate columns to fields property
fields: { value: 'ContactName' },
//set the placeholder to AutoComplete input
placeholder: "Find a customer",
//set the filterType to searching operation
filterType: 'StartsWith',
//sort the resulted items
sortOrder: 'Ascending'
});
filter.appendTo('#atcelement');
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 AutoComplete</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="styles.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-inputs/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-dropdowns/styles/material.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>LOADING....</div>
<div id='container' style="margin:0 auto; width:250px;">
<br>
<input type="text" id='atcelement' />
</div>
</body>
</html>
Filter item count
You can specify the filter suggestion item count through suggestionCount
property of AutoComplete.
The following example, to restrict the suggestion list item counts as 5.
import { AutoComplete } from '@syncfusion/ej2-dropdowns';
//import DataManager related classes
import { Query, DataManager, ODataV4Adaptor } from '@syncfusion/ej2-data';
//initiates the component
let filter: AutoComplete = new AutoComplete({
//bind the DataManager instance to dataSource property
dataSource: new DataManager({
url: 'https://services.odata.org/V4/Northwind/Northwind.svc/Customers',
adaptor: new ODataV4Adaptor,
crossDomain: true
}),
//bind the Query instance to query property
query: new Query().select(['ContactName', 'CustomerID']),
//set the suggestionCount to show the maximum suggestion list item
suggestionCount: 5,
//map the appropriate columns to fields property
fields: { value: 'ContactName' },
//set the placeholder to AutoComplete input
placeholder: "Find a customer",
//sort the resulted items
sortOrder: 'Ascending'
});
filter.appendTo('#atcelement');
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 AutoComplete</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="styles.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-inputs/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-dropdowns/styles/material.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>LOADING....</div>
<div id='container' style="margin:0 auto; width:250px;">
<br>
<input type="text" id='atcelement' />
</div>
</body>
</html>
Limit the minimum filter character
You can set the limit for the character count to filter the data on the AutoComplete. This can be done by set the minLength
property to AutoComplete.
In the following example, the remote request doesn’t fetch the search data, until the search key contains three characters.
import { AutoComplete } from '@syncfusion/ej2-dropdowns';
//import DataManager related classes
import { Query, DataManager, ODataV4Adaptor } from '@syncfusion/ej2-data';
//initiates the component
let filter: AutoComplete = new AutoComplete({
//bind the DataManager instance to dataSource property
dataSource: new DataManager({
url: 'https://services.odata.org/V4/Northwind/Northwind.svc/Customers',
adaptor: new ODataV4Adaptor,
crossDomain: true
}),
//bind the Query instance to query property
query: new Query().select(['ContactName', 'CustomerID']),
//set the minLength to restrict the remote request until search key contains 3 characters.
minLength: 3,
//map the appropriate columns to fields property
fields: { value: 'ContactName' },
//set the placeholder to AutoComplete input
placeholder: "Find a customer",
//set the filterType to searching operation
filterType: 'StartsWith',
//sort the resulted items
sortOrder: 'Ascending'
});
filter.appendTo('#atcelement');
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 AutoComplete</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="styles.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-inputs/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-dropdowns/styles/material.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>LOADING....</div>
<div id='container' style="margin:0 auto; width:250px;">
<br>
<input type="text" id='atcelement' />
</div>
</body>
</html>
Case sensitive filtering
Data items can be filtered either with or without case sensitivity using the DataManager. This can be done by setting the ignoreCase
property of AutoComplete.
The following sample depicts how to filter the data with case-sensitive.
import { AutoComplete } from '@syncfusion/ej2-dropdowns';
//import DataManager related classes
import { Query, DataManager, WebApiAdaptor } from '@syncfusion/ej2-data';
//initiates the component
let filter: AutoComplete = new AutoComplete({
//bind the DataManager instance to dataSource property
dataSource: new DataManager({
url: 'https://js.syncfusion.com/demos/ejServices/Wcf/Northwind.svc/',
crossDomain: true
}),
//bind the Query instance to query property
query: new Query().from('Customers').select('ContactName').take(7),
//map the appropriate columns to fields property
fields: { value: 'ContactName' },
//set the placeholder to AutoComplete input
placeholder: "Find a customer",
//set the ignoreCase as false to enable the case sensitive filtering
ignoreCase: false,
//set the filterType to searching operation
filterType: 'StartsWith',
//sort the resulted items
sortOrder: 'Ascending'
});
filter.appendTo('#atcelement');
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 AutoComplete</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="styles.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-inputs/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-dropdowns/styles/material.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>LOADING....</div>
<div id='container' style="margin:0 auto; width:250px;">
<br>
<input type="text" id='atcelement' />
</div>
</body>
</html>
Diacritics Filtering
An AutoComplete supports diacritics filtering which will ignore the diacritics and makes it easier to filter the results in international characters lists when the ignoreAccent is enabled.
In the following sample,data with diacritics are bound as dataSource for AutoComplete.
import { AutoComplete } from '@syncfusion/ej2-dropdowns';
// create local data
let 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'];
// initialize AutoComplete component
let atcObj: AutoComplete = new AutoComplete({
//set the local data to dataSource property
dataSource: data,
// set the placeholder to AutoComplete input element
placeholder: 'e.g: aero',
// enabled the ignoreAccent property for ignore the diacritics
ignoreAccent: true
});
atcObj.appendTo('#atcelement');
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 AutoComplete</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="styles.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-inputs/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-dropdowns/styles/material.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>LOADING....</div>
<div id='container' style="margin:0 auto; width:250px;">
<br>
<input type="text" id='atcelement' />
</div>
</body>
</html>