Filtering in EJ2 JavaScript AutoComplete
4 Sep 202620 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
The filterType property determines how the component matches the suggested items during a search. The available filter types 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 shows how data is filtered using the StartsWith filter type.
//initiates the component
var acObject = new ej.dropdowns.AutoComplete({
//bind the data manager instance to dataSource property
dataSource: new ej.data.DataManager({
url: 'https://services.odata.org/V4/Northwind/Northwind.svc/Customers',
adaptor: new ej.data.ODataV4Adaptor(),
crossDomain: true
}),
//bind the Query instance to query property
query: new ej.data.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'
});
//render the component
acObject.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/34.2.2/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-inputs/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-dropdowns/styles/material.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/34.2.2/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container" style="margin:0 auto; width:250px;">
<br>
<input type="text" id="atcelement">
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>Filter item count
You can specify the filter suggestion item count through the suggestionCount property of the AutoComplete.
The following example restricts the suggestion list to five items.
//initiates the component
var acObject = new ej.dropdowns.AutoComplete({
//bind the data manager instance to dataSource property
dataSource: new ej.data.DataManager({
url: 'https://services.odata.org/V4/Northwind/Northwind.svc/Customers',
adaptor: new ej.data.ODataV4Adaptor(),
crossDomain: true
}),
//bind the Query instance to query property
query: new ej.data.Query().select(['ContactName']),
//map the appropriate columns to fields property
fields: { value: 'ContactName'},
//set the suggestionCount to show the maximum suggestion list item
suggestionCount: 5,
//set the placeholder to AutoComplete input
placeholder: "Find a customer",
//set the filterType to searching operation
filterType: 'StartsWith',
//sort the resulted items
sortOrder: 'Ascending'
});
//render the component
acObject.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/34.2.2/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-inputs/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-dropdowns/styles/material.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/34.2.2/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container" style="margin:0 auto; width:250px;">
<br>
<input type="text" id="atcelement">
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>Limit the minimum filter character
You can set the minimum number of characters required to filter the data in the AutoComplete. This is done by setting the minLength property of the AutoComplete.
In the following example, the remote request does not fetch the search data until the search key contains three characters.
//initiates the component
var acObject = new ej.dropdowns.AutoComplete({
//bind the data manager instance to dataSource property
dataSource: new ej.data.DataManager({
url: 'https://services.odata.org/V4/Northwind/Northwind.svc/Customers',
adaptor: new ej.data.ODataV4Adaptor(),
crossDomain: true
}),
//bind the Query instance to query property
query: new ej.data.Query().select(['ContactName']),
//map the appropriate columns to fields property
fields: { value: 'ContactName'},
//set the minLength to restrict the remote request until search key contains 3 characters.
minLength: 3,
//set the placeholder to AutoComplete input
placeholder: "Find a customer",
//set the filterType to searching operation
filterType: 'StartsWith',
//sort the resulted items
sortOrder: 'Ascending'
});
//render the component
acObject.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/34.2.2/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-inputs/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-dropdowns/styles/material.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/34.2.2/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container" style="margin:0 auto; width:250px;">
<br>
<input type="text" id="atcelement">
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>Case-sensitive filtering
Data items can be filtered either with or without case sensitivity. This is controlled by the ignoreCase property of the AutoComplete.
The following sample demonstrates how to filter data with case-sensitive matching.
//initiates the component
var acObject = new ej.dropdowns.AutoComplete({
//bind the data manager instance to dataSource property
dataSource: new ej.data.DataManager({
url: 'https://services.odata.org/V4/Northwind/Northwind.svc/Customers',
adaptor: new ej.data.ODataV4Adaptor(),
crossDomain: true
}),
//bind the Query instance to query property
query: new ej.data.Query().select(['ContactName']),
//map the appropriate columns to fields property
fields: { value: 'ContactName'},
//set the ignoreCase as false to enable the case sensitive filtering
ignoreCase: false,
//set the placeholder to AutoComplete input
placeholder: "Find a customer",
//set the filterType to searching operation
filterType: 'StartsWith',
//sort the resulted items
sortOrder: 'Ascending'
});
//render the component
acObject.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/34.2.2/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-inputs/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-dropdowns/styles/material.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/34.2.2/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container" style="margin:0 auto; width:250px;">
<br>
<input type="text" id="atcelement">
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>Diacritics Filtering
The AutoComplete supports diacritics filtering, which ignores 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.
// create local data
var data = [
'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
var atcObj = new ej.dropdowns.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/34.2.2/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-inputs/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-dropdowns/styles/material.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/34.2.2/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container" style="margin:0 auto; width:250px;">
<br>
<input type="text" id="atcelement">
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>Debounce delay
You can use the debounceDelay property for filtering, enabling you to set a delay in milliseconds. This functionality helps reduce the frequency of filtering as you type, enhancing performance and responsiveness for a smoother user experience. By default, a debounceDelay of 300 ms is applied. If you wish to disable this feature entirely, you can set it to 0 ms.
let sportsData = [
{ Id: 'Game1', Game: 'Badminton' },
{ Id: 'Game2', Game: 'Basketball' },
{ Id: 'Game3', Game: 'Cricket' },
{ Id: 'Game4', Game: 'Football' },
{ Id: 'Game5', Game: 'Golf' },
{ Id: 'Game6', Game: 'Hockey' },
{ Id: 'Game7', Game: 'Rugby' },
{ Id: 'Game8', Game: 'Snooker' }
];
// initialize AutoComplete component
var listObj = new ej.dropdowns.AutoComplete({
//set the data to dataSource property
dataSource: sportsData,
// maps the appropriate column to fields property
fields: { value: 'Game' },
//set the debounceDelay
debounceDelay :'300',
// set placeholder to AutoComplete input element
placeholder: "Find a game"});
listObj.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/34.2.2/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-inputs/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-dropdowns/styles/material.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/34.2.2/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container" style="margin:0 auto; width:250px;">
<br>
<input type="text" id="atcelement">
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>