Search results

Filtering

The DropDownList 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 search box.

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

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

razor
filter.cs
@Html.EJS().DropDownList("customers").Placeholder("Select a customer").Filtering("onfiltering").PopupHeight("200px").DataSource(dataManger =>
            dataManger.Url("http://services.odata.org/V4/Northwind/Northwind.svc/Customers").Adaptor("ODataV4Adaptor").CrossDomain(true)).AllowFiltering(true).Fields(new DropDownListFieldSettings
            {
                Value = "ContactName",                
            }).Query((string)ViewBag.query).Render()
<script type="text/javascript">
    function onfiltering(e) {
        var obj = document.getElementById("customers").ej2_instances[0];
            var query = new ej.data.Query();
            query = (e.text !== '') ? query.where('ContactName', 'startswith', e.text, true) : query;
            e.updateData(obj.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 DropDownListController : Controller
    {
        public IActionResult filter()
        {
            ViewBag.data = new Countries().CountriesList();
            return View();
        }       
    }
}

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 DropDownList. This can be done by manual validation within the filter event handler.

In the following example, the remote request does not fetch the search data until the search key contains three characters.

razor
filterlimit.cs
@Html.EJS().DropDownList("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 DropDownListFieldSettings
            {
                Value = "ContactName",
            }).Query((string)ViewBag.query).Render()

<script type="text/javascript">
    function onfiltering(e) {
        var data = document.getElementById('customers').ej2_instances[0];
         // load overall data when search key empty.
        if (e.text == '')
            e.updateData(data.dataSource);
        else {
            // restrict the remote request until search key contains 3 characters.
            if (e.text.length < 3) {
                return;
            }
            let query = new ej.data.Query().select(['ContactName', 'CustomerID']).take(7);
            query = (e.text !== '') ? query.where('ContactName', 'startswith', 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 DropDownListController : Controller
    {
        public IActionResult filteringlimit()
        {
            return View();
        }
    }
}

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.

razor
filtertype.cs
@Html.EJS().DropDownList("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 DropDownListFieldSettings
            {
                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 DropDownListController : Controller
    {
        public IActionResult filtertype()
        {
            return View();
        }       
    }
}

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.

The following example shows how to perform case-sensitive filter.

razor
casesensitive.cs
@Html.EJS().DropDownList("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 DropDownListFieldSettings
            {
                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 DropDownListController : Controller
    {
        public IActionResult casesensitive()
        {
            return View();
        }       
    }
}

Diacritics Filtering

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

razor
diacritics.cs
@Html.EJS().DropDownList("list").DataSource((string[])ViewBag.data).Placeholder("Select a value").FilterBarPlaceholder("e.g: aero").AllowFiltering(true).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 DropDownListController : Controller
    {
        public IActionResult diacritics()
        {
            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();
        }       
    }
}