Search results

Filtering

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
StartsWith Checks whether a value begins with the specified value.
EndsWith Checks whether a value ends with specified value.
Contains Checks whether a value contains with specified value.

The following examples shows the data filtering is done with StartsWith type.

razor
filtertype.cs
@Html.EJS().AutoComplete("customers").Placeholder("Select a customer").PopupHeight("200px").AllowFiltering(true).DataSource(dataManger =>
            dataManger.Url("http://services.odata.org/V4/Northwind/Northwind.svc/Customers").Adaptor("ODataV4Adaptor").CrossDomain(true)).AllowFiltering(true).Fields(new AutoCompleteFieldSettings
            {
                Value = "ContactName",
            }).Query((string)ViewBag.query).Render()

<script type="text/javascript">
    function onfiltering(e) {
        var data = document.getElementById('customers').ej2_instances[0];
        if (e.text == '') e.updateData(data.dataSource);
        else {
            var query = new ej.data.Query().select('CustomerID', 'ContactName').take(7);
            // change the type of filtering
            query = (e.text !== '') ? query.where('CustomerID', 'endswith', e.text, true) : query;
            e.updateData(data.dataSource, query);
        }
    }
</script>
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()
        {
            return View();
        }       
    }
}

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.

razor
filtercount.cs
@Html.EJS().AutoComplete("customers").Placeholder("Select a customer").PopupHeight("200px").SuggestionCount(5).DataSource(dataManger =>
            dataManger.Url("http://services.odata.org/V4/Northwind/Northwind.svc/Customers").Adaptor("ODataV4Adaptor").CrossDomain(true)).AllowFiltering(true).Fields(new AutoCompleteFieldSettings
            {
                Value = "ContactName",
            }).Query((string)ViewBag.query).Render()
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()
        {
            return View();
        }       
    }
}

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.

razor
filterlimit.cs
@Html.EJS().AutoComplete("customers").Placeholder("Select a customer").PopupHeight("200px").MinLength(3).DataSource(dataManger =>
            dataManger.Url("http://services.odata.org/V4/Northwind/Northwind.svc/Customers").Adaptor("ODataV4Adaptor").CrossDomain(true)).AllowFiltering(true).Fields(new AutoCompleteFieldSettings
            {
                Value = "ContactName",
            }).Query((string)ViewBag.query).Render()
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()
        {
            return View();
        }       
    }
}

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.

razor
casesensitive.cs
@Html.EJS().AutoComplete("customers").Placeholder("Select a customer").PopupHeight("200px").AllowFiltering(true).DataSource(dataManger =>
            dataManger.Url("http://services.odata.org/V4/Northwind/Northwind.svc/Customers").Adaptor("ODataV4Adaptor").CrossDomain(true)).AllowFiltering(true).Fields(new AutoCompleteFieldSettings
            {
                Value = "ContactName",
            }).Query((string)ViewBag.query).Render()

<script type="text/javascript">
    function onfiltering(e) {
        var data = document.getElementById('customers').ej2_instances[0];
        if (e.text == '') e.updateData(data.dataSource);
        else {
            var query = new ej.data.Query().select('CustomerID', 'ContactName').take(10);
            // change the type of filtering
            query = (e.text !== '') ? query.where('ContactName', 'startswith', e.text, false) : query;
            e.updateData(data.dataSource, query);
        }
    }
</script>
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()
        {
            return View();
        }       
    }
}

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.

razor
diacritics.cs
@Html.EJS().AutoComplete("Diacritics").DataSource((string[])ViewBag.data).Placeholder("e.g: aero").IgnoreAccent(true).Render()
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();
        }       
    }
}