Example of Custom Filtering in ASP.NET MVC AutoComplete Control
This sample demonstrates the custom filtering functionalities of the AutoComplete. You can choose an item from the suggestion list that filtered items based on approximate string matching technique.
@Html.UiAutoComplete("books", new UiAutoCompleteOptions
{
Placeholder = "e.g. Node.js Succinctly",
OnFiltering = "filtering",
DataSource = (IEnumerable<DropdownsDemoController.BooksData>)ViewData["data"],
Fields = new UiDropDownFields { Text = "BookName" },
DebounceDelay = 300
})
@Html.UiNumericTextBox("numeric", new UiNumericTextBoxOptions { Format = "n0", Value = 300, Min = 1, OnChange = "change" })
<style>
.control-wrapper { margin: 0 auto; width: 250px; }
.example-label { display: block; margin-bottom: 4px; }
</style>
@using Lumen.UI.Mvc.Demo.Controllers
<div class="control-section" style="height: 400px;">
<div class="control-wrapper">
<div style="padding-top:75px;">
@Html.UiAutoComplete("books", new UiAutoCompleteOptions
{
Placeholder = "e.g. Node.js Succinctly",
OnFiltering = "filtering",
DataSource = (IEnumerable<DropdownsDemoController.BooksData>)ViewData["data"],
Fields = new UiDropDownFields { Text = "BookName" },
DebounceDelay = 300
})
</div>
</div>
</div>
<div class="demo-property-panel" title="Properties">
<div class="demo-property-row">
<label class="example-label">Debounce Delay</label>
@Html.UiNumericTextBox("numeric", new UiNumericTextBoxOptions { Format = "n0", Value = 300, Min = 1, OnChange = "change" })
</div>
</div>
<script>
var books = @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(((IEnumerable<DropdownsDemoController.BooksData>)ViewData["data"]).Select(b => b.BookName)));
// Syncfusion searches with Fuse.js; here a book matches when the typed letters appear in it in the same order (a fuzzy match).
function filtering(args) {
var text = (args.text || '').toLowerCase();
return books.filter(function (name) {
var at = 0, lower = name.toLowerCase();
for (var i = 0; i < text.length; i++) {
at = lower.indexOf(text.charAt(i), at);
if (at < 0) { return false; }
at++;
}
return true;
}).map(function (name) { return { value: name, text: name }; });
}
function change(value) {
window.Lumen.get('books').setDebounceDelay(value);
}
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class AutoCompleteController : Controller
{
// The page is Views/AutoComplete/CustomFiltering.cshtml; it draws what ViewData holds.
public ActionResult CustomFiltering()
{
ViewData["data"] = new BooksData().GetBooksData();
return View();
}
public class BooksData
{
public string BookName { get; set; }
public string BookID { get; set; }
public List<BooksData> GetBooksData()
{
List<BooksData> book = new List<BooksData>();
book.Add(new BooksData { BookName = "Support Vector Machines Succinctly", BookID = "BOOK1" });
book.Add(new BooksData { BookName = "Scala Succinctly", BookID = "BOOK2" });
book.Add(new BooksData { BookName = "Application Security in .NET Succinctly", BookID = "BOOK3" });
// ... 57 more rows
return book;
}
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
The AutoComplete can be customized to showcase the suggestion list by using filtering event. In that, you can use your own libraries to filter the data and update it to AutoComplete suggestion list via updateData method. The debounce delay, in milliseconds, for filtering items in the AutoComplete component can be set using the DebounceDelay property.
In this sample, used Fuse.js library for custom filtering of books data.