Example of Grouping and Icon in ASP.NET MVC AutoComplete Control
This sample demonstrates the grouping and icons supports to the AutoComplete. Type a character in the AutoComplete element and choose an item from the categorized list/icons list.
Icons
@Html.UiAutoComplete("vegetables", new UiAutoCompleteOptions
{
DataSource = (IEnumerable<DropdownsDemoController.Vegetables>)ViewData["groupdata"],
ShowPopupButton = true,
Placeholder = "e.g. Cabbage",
Fields = new UiDropDownFields { GroupBy = "Category", Text = "Vegetable" }
})
@Html.UiAutoComplete("icons", new UiAutoCompleteOptions
{
DataSource = (IEnumerable<DropdownsDemoController.SocialMedia>)ViewData["icondata"],
ShowPopupButton = true,
Placeholder = "e.g. Facebook",
Fields = new UiDropDownFields { Text = "SocialMediaName", IconCss = "Class" }
})
<style>
.control-section .content { margin: 0 auto; width: 250px; }
.socialicon { display: inline-block; width: 18px; text-align: center; font-weight: 700; font-size: 14px; }
.facebook::before { content: "f"; }
.google-plus::before { content: "g+"; }
.instagram::before { content: "\25A3"; }
.linkedin::before { content: "in"; }
.skype::before { content: "s"; }
.tumblr::before { content: "t"; }
.twitter::before { content: "t"; }
.vimeo::before { content: "v"; }
.whatsapp::before { content: "w"; }
.youtube::before { content: "\25B6"; }
</style>
@using Lumen.UI.Mvc.Demo.Controllers
<div class="control-section">
<div id="groupList" style="padding-top:15px">
<div class="content">
<h4>Grouping</h4>
@Html.UiAutoComplete("vegetables", new UiAutoCompleteOptions
{
DataSource = (IEnumerable<DropdownsDemoController.Vegetables>)ViewData["groupdata"],
ShowPopupButton = true,
Placeholder = "e.g. Cabbage",
Fields = new UiDropDownFields { GroupBy = "Category", Text = "Vegetable" }
})
</div>
</div>
<div id="iconList" style="padding-top:15px">
<div class="content">
<h4> Icons</h4>
@Html.UiAutoComplete("icons", new UiAutoCompleteOptions
{
DataSource = (IEnumerable<DropdownsDemoController.SocialMedia>)ViewData["icondata"],
ShowPopupButton = true,
Placeholder = "e.g. Facebook",
Fields = new UiDropDownFields { Text = "SocialMediaName", IconCss = "Class" }
})
</div>
</div>
</div>
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/GroupingAndIcon.cshtml; it draws what ViewData holds.
public ActionResult GroupingAndIcon()
{
ViewData["groupdata"] = new Vegetables().VegetablesList();
ViewData["icondata"] = new SocialMedia().SocialMediaList();
return View();
}
public class Vegetables
{
public string Vegetable { get; set; }
public string Category { get; set; }
public string Id { get; set; }
public List<Vegetables> VegetablesList()
{
List<Vegetables> veg = new List<Vegetables>();
veg.Add(new Vegetables { Vegetable = "Cabbage", Category= "Leafy and Salad", Id= "item1" });
veg.Add(new Vegetables { Vegetable = "Chickpea", Category= "Beans", Id= "item2" });
veg.Add(new Vegetables { Vegetable = "Garlic", Category= "Bulb and Stem", Id= "item3" });
veg.Add(new Vegetables { Vegetable = "Green bean", Category= "Beans", Id= "item4" });
veg.Add(new Vegetables { Vegetable = "Horse gram", Category= "Beans", Id= "item5" });
veg.Add(new Vegetables { Vegetable = "Nopal", Category= "Bulb and Stem", Id= "item6" });
veg.Add(new Vegetables { Vegetable = "Onion", Category= "Bulb and Stem", Id= "item7" });
veg.Add(new Vegetables { Vegetable = "Pumpkins", Category= "Leafy and Salad", Id= "item8" });
veg.Add(new Vegetables { Vegetable = "Spinach", Category= "Leafy and Salad", Id= "item9" });
veg.Add(new Vegetables { Vegetable = "Wheat grass", Category= "Leafy and Salad", Id= "item10" });
veg.Add(new Vegetables { Vegetable = "Yarrow", Category = "Leafy and Salad", Id = "item11" });
return veg;
}
}
public class SocialMedia
{
public string Class { get; set; }
public string SocialMediaName { get; set; }
public string Id { get; set; }
public List<SocialMedia> SocialMediaList()
{
List<SocialMedia> media = new List<SocialMedia>();
media.Add(new SocialMedia{ Class= "facebook", SocialMediaName= "Facebook", Id= "media1" });
media.Add(new SocialMedia{ Class= "google-plus", SocialMediaName= "Google Plus", Id= "media2" });
media.Add(new SocialMedia{ Class= "instagram", SocialMediaName= "Instagram", Id= "media3" });
media.Add(new SocialMedia{ Class= "linkedin", SocialMediaName= "LinkedIn", Id= "media4" });
media.Add(new SocialMedia{ Class= "skype", SocialMediaName= "Skype", Id= "media5" });
media.Add(new SocialMedia{ Class= "tumblr", SocialMediaName= "Tumblr", Id= "media6" });
media.Add(new SocialMedia{ Class= "twitter", SocialMediaName= "Twitter", Id= "media7" });
media.Add(new SocialMedia{ Class= "vimeo", SocialMediaName= "Vimeo", Id= "media8" });
media.Add(new SocialMedia{ Class= "whatsapp", SocialMediaName= "WhatsApp", Id= "media9" });
media.Add(new SocialMedia { Class = "youtube", SocialMediaName= "YouTube", Id = "media10" });
return media;
}
}
}
}
@* 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 allows to group the relevant items under a corresponding category by mapping the groupBy field, and allows to load the list items with icons. And also enabled the showPopupButton property to show the all suggestion items while clicking on popup button.
The grouping sample illustrates how the vegetables are grouped based on its category.
The 2nd AutoComplete is populated with icons which is rendered by mapping the iconCss field.