Example of Group Template in ASP.NET MVC ListView Control
This sample demonstrates the group template functionalities of ListView. Click any list item from the settings option to select and highlight an option.
${Initial}${Text}${Content}
${Text}
Settings
- Device
- Display
- Sound
- Battery
- Users
- Personal
- Location
- Security
- Languages & input
- Wireless & networks
- WI-FI
- Bluetooth
- SIM cards
@Html.UiListView(new UiListViewOptions { Id = "groupedList", ShowHeader = true, HeaderTitle = "Settings", Template = "#settingTemplate", GroupTemplate = "#settingGroup", Items = (List<UiListItem>)ViewData["dataSource"] })
<style>
.control-section { max-width: 700px; margin: 0 auto; padding: 10px 20px; }
.control-section h4 { margin: 14px 0 6px; }
#flat-list, #group-list { display: inline-block; vertical-align: top; width: 48%; min-width: 260px; margin-right: 1%; }
.lv-row { display: grid; grid-template-columns: 44px 1fr; column-gap: 12px; align-items: center; width: 100%; box-sizing: border-box; padding: 6px 4px; }
.lv-contact { grid-template-columns: 44px 1fr; }
.lv-avatar { display: inline-flex; width: 36px; height: 36px; align-items: center; justify-content: center; border-radius: 50%; background: var(--ui-row-hover-bg); font-size: 14px; font-weight: 600; flex: none; }
.lv-setting .lv-avatar, .lv-call .lv-avatar { grid-row: span 2; }
.setting-name { font-weight: 600; }
.setting-content { color: var(--ui-muted); font-size: 13px; }
.group-name { color: var(--ui-accent); font-weight: 600; }
</style>
<template id="settingTemplate"><div class="lv-row lv-setting"><span class="lv-avatar">${Initial}</span><span class="setting-name">${Text}</span><span class="setting-content">${Content}</span></div></template>
<template id="settingGroup"><span class="group-name">${Text}</span></template>
<div class="control-section">
@Html.UiListView(new UiListViewOptions { Id = "groupedList", ShowHeader = true, HeaderTitle = "Settings", Template = "#settingTemplate", GroupTemplate = "#settingGroup", Items = (List<UiListItem>)ViewData["dataSource"] })
</div>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class ListViewController : Controller
{
// The page is Views/ListView/GroupTemplate.cshtml; it draws what ViewData holds.
public ActionResult GroupTemplate()
{
ViewData["dataSource"] = new List<UiListItem>
{
new UiListItem { Value = "1", Text = "WI-FI", GroupBy = "Wireless & networks", Data = new { Initial = "W", Content = "Disabled" } },
new UiListItem { Value = "2", Text = "Bluetooth", GroupBy = "Wireless & networks", Data = new { Initial = "B", Content = "Disabled" } },
new UiListItem { Value = "3", Text = "SIM cards", GroupBy = "Wireless & networks", Data = new { Initial = "S", Content = "AT&T" } },
new UiListItem { Value = "4", Text = "Display", GroupBy = "Device", Data = new { Initial = "D", Content = "Adaptive brightness is OFF" } },
new UiListItem { Value = "5", Text = "Sound", GroupBy = "Device", Data = new { Initial = "S", Content = "Ringer volume at 50%" } },
new UiListItem { Value = "6", Text = "Battery", GroupBy = "Device", Data = new { Initial = "B", Content = "85%" } },
new UiListItem { Value = "7", Text = "Users", GroupBy = "Device", Data = new { Initial = "U", Content = "Signed in as Albert" } },
new UiListItem { Value = "8", Text = "Location", GroupBy = "Personal", Data = new { Initial = "L", Content = "ON / High accuracy" } },
new UiListItem { Value = "9", Text = "Security", GroupBy = "Personal", Data = new { Initial = "S", Content = "Screen Lock" } },
new UiListItem { Value = "10", Text = "Languages & input", GroupBy = "Personal", Data = new { Initial = "L", Content = "English (US)" } }
};
return View();
}
}
}
@* 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 ListView component has an option to custom design the group header title with the help of GroupTemplate property.
In this example, both the group header and list items are customized using the GroupTemplate and Template property.