Search results

Filtering

The MultiSelect 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 MultiSelect input.

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

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

tagHelper
filter.cs
Countries.cs
<div class="col-lg-12 control-section" style="height: 400px;">
        <div class='control-wrapper' style="margin: 0 auto;width:250px;">
            <h4>Filtering</h4>
            <ejs-multiselect id="list" placeholder="Select countries" dataSource="@ViewBag.data"
                            allowFiltering="true" filtering="onFiltering">
                <e-multiselect-fields text="Name" value="Code"></e-multiselect-fields>
            </ejs-multiselect>
        </div>
    </div>
    <script type="text/javascript">
        function onFiltering(e) {
            var query = new ej.data.Query();
            // frame the query based on search string with filter type.
            query = (e.text !== '') ? query.where('Name', 'startswith', e.text, true) : query;
            // pass the filter data source, filter query to updateData method.
            e.updateData(@Html.Raw(JsonConvert.SerializeObject(ViewBag.data)), 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 MultiSelectController : Controller
    {
        public IActionResult filter()
        {            
            ViewBag.data = new Countries().CountriesList();
            return View();      
        }       
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebApplication1.Models
{
    public class Countries
    {
        public string Name { get; set; }
        public string Code { get; set; }
        public List<Countries> CountriesList()
        {
            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" });
            return country;
        }
    }
}

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

tagHelper
filterlimit.cs
<div class="col-lg-12 control-section" style="height: 400px;">
    <div class='control-wrapper' style="margin: 0 auto;width:250px;">
        <ejs-multiselect id="customers" placeholder="Select a customer" popupHeight="200px" allowFiltering="true" filtering="onfiltering" query="new ej.data.Query().select(['ContactName', 'CustomerID']).take(6)">
            <e-multiselect-fields value="CustomerID" text="ContactName"></e-multiselect-fields>
            <e-datamanager url="http://services.odata.org/V4/Northwind/Northwind.svc/Customers" adaptor="ODataV4Adaptor" crossDomain="true"></e-datamanager>
        </ejs-multiselect>
    </div>
</div>
<script type="text/javascript">
    function onFiltering(e) {
        var data = document.getElementById("customer").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;
            }
            var query = new ej.data.Query().select(['ContactName', 'CustomerID']).take(6);
            query = (e.text !== '') ? query.where('Name', '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 MultiSelectController : Controller
    {
        public IActionResult filterlimit()
        {
            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.

tagHelper
filtertype.cs
<div class="col-lg-12 control-section" style="height: 400px;">
    <div class='control-wrapper' style="margin: 0 auto;width:250px;">
        <ejs-multiselect id="customers" placeholder="Select a customer" popupHeight="200px" allowFiltering="true" filtering="onfiltering" query="new ej.data.Query().select(['ContactName', 'CustomerID']).take(6)">
            <e-multiselect-fields value="CustomerID" text="ContactName"></e-multiselect-fields>
            <e-datamanager url="http://services.odata.org/V4/Northwind/Northwind.svc/Customers" adaptor="ODataV4Adaptor" crossDomain="true"></e-datamanager>
        </ejs-multiselect>
    </div>
</div>
<script type="text/javascript">
    function onFiltering(e) {
        var data = document.getElementById("customer").ej2_instances[0];
        // load overall data when search key empty.
        if (e.text == '')
            e.updateData(data.dataSource);
        else {
            var query = new ej.data.Query().select(['ContactName', 'CustomerID']).take(6);
            query = (e.text !== '') ? query.where('ContactName', '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 MultiSelectController : 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.

tagHelper
casesensitive.cs
<div class="col-lg-12 control-section" style="height: 400px;">
    <div class='control-wrapper' style="margin: 0 auto;width:250px;">
        <ejs-multiselect id="customers" placeholder="Select a customer" popupHeight="200px" allowFiltering="true" filtering="onfiltering" query="new ej.data.Query().select(['ContactName', 'CustomerID']).take(6)">
            <e-multiselect-fields value="CustomerID" text="ContactName"></e-multiselect-fields>
            <e-datamanager url="http://services.odata.org/V4/Northwind/Northwind.svc/Customers" adaptor="ODataV4Adaptor" crossDomain="true"></e-datamanager>
        </ejs-multiselect>
    </div>
</div>
<script type="text/javascript">
    function onFiltering(e) {
        var data = document.getElementById("customer").ej2_instances[0];
        // load overall data when search key empty.
        if (e.text == '')
            e.updateData(data.dataSource);
        else {
            var query = new ej.data.Query().select(['ContactName', 'CustomerID']).take(6);
            query = (e.text !== '') ? query.where('ContactName', 'endswith', 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 MultiSelectController : Controller
    {
        public IActionResult casesensitive()
        {
            return View();
        }       
    }
}

Diacritics Filtering

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

tagHelper
diacritics.cs
<div>
    <ejs-multiselect id="list" dataSource="@ViewBag.data" placeholder="e.g: aero" mode="box">
    </ejs-multiselect>
</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 MultiSelectController : 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();
        }       
    }
}