Highlight filtering in EJ2 TypeScript Drop down list control
2 May 20234 minutes to read
By using the highlightSearch method, you can highlight the matched character in DropDownList filtering.
The following example demonstrates about how to highlight the matched character in filtering.
// import highlightSearch module from ej2 dropdown package
import { DropDownList, FilteringEventArgs, highlightSearch } from '@syncfusion/ej2-dropdowns';
import { Query, DataManager, ODataV4Adaptor } from '@syncfusion/ej2-data';
let searchData: DataManager = new DataManager({
url: 'https://services.odata.org/V4/Northwind/Northwind.svc/Customers',
adaptor: new ODataV4Adaptor,
crossDomain: true
});
let text: string;
let filter: DropDownList = new DropDownList({
dataSource: searchData,
query: new Query().select(['ContactName', 'CustomerID']).take(7),
// map the appropriate column
fields: <{ [key: string]: Object }>{
text: 'ContactName', value: 'CustomerID', itemCreated: (e: { [key: string]: Object }) => {
highlightSearch(e.item, text, true, 'StartsWith');
}
},
// set placeholder to DropDownList input element
placeholder: "Select a name",
// set true to allowFiltering for enable filtering supports
allowFiltering: true,
//bind the filtering event handler
filtering: (e: FilteringEventArgs) => {
// take text for highlight the character in list items.
text = e.text;
let query: Query = new Query().select(['ContactName', 'CustomerID']);
//frame the query based on search string with filter type.
query = (e.text !== '') ? query.where('ContactName', 'startswith', e.text, true) : query;
//pass the filter data source, filter query to updateData method.
e.updateData(searchData, query);
}
});
filter.appendTo('#ddlelement');
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 DropDownList</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='ddlelement' />
</div>
</body>
</html>