Filtering in Combo Box Control

19 Jun 20249 minutes to read

The ComboBox has built-in support to filter data items when allowFiltering is enabled. The filter operation starts as soon as you start typing characters in the control.

To display filtered items in the popup, filter the required data and return it to the ComboBox via updateData method by using the filtering event.

The following sample illustrates how to query the data source and pass the data to the ComboBox through the updateData method in filtering event.

@using Newtonsoft.Json
@{
    List<Countries> country = new List<Countries>();
    country.Add(new Countries { Name = "Australia", Code = "AU" });
    country.Add(new Countries { Name = "Bermuda", Code = "BM" });
    country.Add(new Countries { Name = "Canada", Code = "CA" });
    country.Add(new Countries { Name = "Cameroon", Code = "CM" });
    country.Add(new Countries { Name = "Denmark", Code = "DK" });
    country.Add(new Countries { Name = "France", Code = "FR" });
    country.Add(new Countries { Name = "Finland", Code = "FI" });
    country.Add(new Countries { Name = "Germany", Code = "DE" });
    country.Add(new Countries { Name = "Greenland", Code = "GL" });
    country.Add(new Countries { Name = "Hong Kong", Code = "HK" });
    country.Add(new Countries { Name = "India", Code = "IN" });
    country.Add(new Countries { Name = "Italy", Code = "IT" });
    country.Add(new Countries { Name = "Japan", Code = "JP" });
    country.Add(new Countries { Name = "Mexico", Code = "MX" });
    country.Add(new Countries { Name = "Norway", Code = "NO" });
    country.Add(new Countries { Name = "Poland", Code = "PL" });
    country.Add(new Countries { Name = "Switzerland", Code = "CH" });
    country.Add(new Countries { Name = "United Kingdom", Code = "GB" });
    country.Add(new Countries { Name = "United States", Code = "US" });
}

<div style='padding-top:75px;'>
    <ejs-combobox id="country" placeholder="Select a country" allowFiltering="true" filtering="onfiltering" dataSource="@country" popupHeight="270px">
        <e-combobox-fields value="Name"></e-combobox-fields>
    </ejs-combobox>
</div>

<script>
    function onfiltering(e) {
         var query = new ej.data.Query();
            query = (e.text !== '') ? query.where('Name', 'startswith', e.text, true) : query;
            e.updateData(@Html.Raw(JsonConvert.SerializeObject(ViewBag.data)), query);

    }
</script>
public class Countries
{
    public string Name { get; set; }
    public string Code { get; set; }
}

Limit the minimum filter character

When filtering the list items, you can set the limit for character count to raise remote request and fetch filtered data on the ComboBox. This can be done by manual validation within the filter event handler.

<div style='padding-top:75px;'>
    <ejs-combobox id="customers" placeholder="Select a customer" popupHeight="200px" allowFiltering="true" filtering="onfiltering" query="new ej.data.Query().from('Customers').select(['ContactName', 'CustomerID']).take(6)">
        <e-combobox-fields value="CustomerID" text="ContactName"></e-combobox-fields>
        <e-data-manager url="https://services.odata.org/V4/Northwind/Northwind.svc/" adaptor="ODataV4Adaptor" crossDomain="true"></e-data-manager>
    </ejs-combobox>
</div>
<script>
    function onfiltering(e) {
        var CBObj = document.getElementById("customers").ej2_instances[0];
        // load overall data when search key empty.
        if (e.text == '' && e.text.length < 3) {
            e.updateData(CBObj.dataSource);
        }
            let query = new ej.data.Query().from('Customers').select(['ContactName', 'CustomerID']).take(6);
            query = (e.text !== '' && e.text.length >= 3) ? query.where('ContactName', 'startswith', e.text, true) : query;
            e.updateData(CBObj.dataSource, query);
    }
</script>

Change the filter type

While filtering, you can change the filter type to contains, startsWith, or endsWith for string type within the filter event handler.

In the following examples, data filtering is done with endsWith type.

<div id='remote-data' class='col-lg-6' style='padding-top:15px'>
    <div class='content'>
        <ejs-combobox id="customers" query="new ej.data.Query().from('Customers').select(['ContactName'])" placeholder="Find a customer" allowFiltering="true" filtering="onfiltering" popupHeight="200px">
            <e-combobox-fields value="ContactName"></e-combobox-fields>
            <e-data-manager url="https://services.odata.org/V4/Northwind/Northwind.svc/" adaptor="ODataV4Adaptor" crossDomain="true"></e-data-manager>
        </ejs-combobox>
    </div>
</div>

<script>
    function onfiltering(e) {
        var CBObj = document.getElementById("customers").ej2_instances[0];
        // load overall data when search key empty.
        if (e.text == '')
            e.updateData(CBObj.dataSource);
        else {
            var query = new ej.data.Query().from('Customers').select(['ContactName', 'CustomerID']).take(6);
            query = (e.text !== '') ? query.where('ContactName', 'endswith', e.text, true) : query;
            e.updateData(CBObj.dataSource, query);
        }
    }
</script>

Case sensitive filtering

Data items can be filtered either with or without case sensitivity using the DataManager. This can be done by passing the fourth optional parameter of the where clause.

<div id='remote-data' class='col-lg-6' style='padding-top:15px'>
    <div class='content'>
        <ejs-combobox id="customers" query="new ej.data.Query().from('Customers').select(['ContactName'])" placeholder="Find a customer" allowFiltering="true" filtering="onfiltering"  popupHeight="200px">
            <e-combobox-fields value="ContactName"></e-combobox-fields>
            <e-data-manager url="https://services.odata.org/V4/Northwind/Northwind.svc/" adaptor="ODataV4Adaptor" crossDomain="true"></e-data-manager>
        </ejs-combobox>
    </div>
</div>
<script>
    function onfiltering(e) {
        var CBObj = document.getElementById("customers").ej2_instances[0];
        // load overall data when search key empty.
        if (e.text == '')
            e.updateData(CBObj.dataSource);
        else {
            var query = new ej.data.Query().from('Customers').select(['ContactName', 'CustomerID']).take(6);
            query = (e.text !== '') ? query.where('ContactName', 'startswith', e.text, false) : query;
            e.updateData(CBObj.dataSource, query);
        }
    }
</script>

Diacritics filtering

ComboBox 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.

@{
    var data = new 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"  };
}

<div class="control-wrapper">
    <div id="default" style='padding-top:75px;'>
        <ejs-combobox id="list" dataSource="@data" ignoreAccent="true" placeholder="e.g: aero">
        </ejs-combobox>
    </div>
</div>

NOTE

View Sample in GitHub.

See also