Example of Highlight in ASP.NET MVC AutoComplete Control
This sample demonstrates the highlight functionalities of the AutoComplete. Type a character(s) in the AutoComplete element and the typed characters are highlighted in the suggestion list. By default, Contains filter type is set in this sample and provided with the options to choose different filter type in the property panel.
@Html.UiAutoComplete("country", new UiAutoCompleteOptions
{
DataSource = (IEnumerable<DropdownsDemoController.CountriesData>)ViewData["countries"],
Placeholder = "e.g. Australia",
Highlight = true,
Fields = new UiDropDownFields { Text = "Name" }
})
@Html.UiDropDownList("filtertype", new UiDropDownListOptions
{
Items = ((string[])ViewData["data"]).Select(text => new UiListItem(text, text)).ToList(),
Value = "Contains",
Placeholder = "Select a type",
Width = "150px",
OnChange = "onchange"
})
<style>
.control-wrapper { margin: 0 auto; width: 250px; }
</style>
@using Lumen.UI.Mvc.Demo.Controllers
<div class="control-section">
<div class="control-wrapper">
<div style="padding-top:75px;">
@Html.UiAutoComplete("country", new UiAutoCompleteOptions
{
DataSource = (IEnumerable<DropdownsDemoController.CountriesData>)ViewData["countries"],
Placeholder = "e.g. Australia",
Highlight = true,
Fields = new UiDropDownFields { Text = "Name" }
})
</div>
</div>
</div>
<div class="demo-property-panel" title="Properties">
<div class="demo-property-row">
<label class="example-label">Filter type</label>
@Html.UiDropDownList("filtertype", new UiDropDownListOptions
{
Items = ((string[])ViewData["data"]).Select(text => new UiListItem(text, text)).ToList(),
Value = "Contains",
Placeholder = "Select a type",
Width = "150px",
OnChange = "onchange"
})
</div>
</div>
<script>
// Syncfusion: the filterType of the AutoComplete is the chosen type.
function onchange(value) {
if (value) { window.Lumen.get('country').setFilterType(value); }
}
window.addEventListener('load', function () { setTimeout(function () { onchange('Contains'); }, 0); });
</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/Highlight.cshtml; it draws what ViewData holds.
public ActionResult Highlight()
{
ViewData["countries"] = new CountriesData().CountriesList();
ViewData["data"] = new string[] { "Contains", "StartsWith", "EndsWith" };
return View();
}
public class CountriesData
{
public string Name { get; set; }
public string Code { get; set; }
public List<CountriesData> CountriesList()
{
List<CountriesData> country = new List<CountriesData>();
country.Add(new CountriesData { Name = "Australia", Code = "AU" });
country.Add(new CountriesData { Name = "Bermuda", Code = "BM" });
country.Add(new CountriesData { Name = "Canada", Code = "CA" });
// ... 16 more rows
return country;
}
}
}
}
@* 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 has built-in support to highlight the searched characters on the suggested list items when highlight is enabled.
This sample illustrates that, the searched characters on the country suggestion list items are highlighted.