Filtering in ASP.NET CORE AutoComplete Control

16 Feb 20245 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 control.

Change the filter type

Determines on which filter type, the control needs to be considered on search action. The available filterType and its supported data types are as follows.

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 examples shows the data filtering is done with StartsWith type.

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

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.

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

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

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

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.

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

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.

@{
    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-autocomplete id="list" dataSource="@data" ignoreAccent="true" placeholder="e.g: aero">
        </ejs-autocomplete>
    </div>
</div>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class AutoCompleteController : Controller
    {
        public IActionResult filtering()
        {
            ViewBag.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"  };
            return View();
        }       
    }
}

NOTE

View Sample in GitHub.

See also