Filter using both text and value field
19 Dec 20222 minutes to read
The AutoComplete data can be filtered based on both text and value fields using predicate
of dataManager through filtering event. The filtered data can be again updated through updateData
method.
@using Newtonsoft.Json;
@Html.EJS().AutoComplete("project").DataSource((IEnumerable<object>)ViewBag.data).Fields(new Syncfusion.EJ2.DropDowns.AutoCompleteFieldSettings { Text = "Name", Value = "Code" }).ItemTemplate("<span class='item'><span class='name'>${Name}</span>-<span class='code'>${Code}</span></span>").Filtering("filtering").Render()
<script>
function filtering(e) {
var predicate = new ej.data.Predicate('Name', 'contains', e.text, true);
predicate = predicate.or('Code', 'contains', e.text);
var query = new ej.data.Query();
query = (e.text !== '' && e.value !== '') ? query.where(predicate) : query;
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 AutoCompleteController : Controller
{
public ActionResult filtering()
{
ViewBag.data = new Countries().CountriesList();
return View();
}
}
}
NOTE