Example of Filter Menu in ASP.NET Core Data Grid Control
This sample demonstrates the filtering capabilities of the Grid using multiple filter types. It shows how users can interact with different filtering options to refine and view data efficiently.
Properties
@Html.AntiForgeryToken()
@Html.UiGrid(new UiGridOptions
{
Id = "FilterMenu",
DataUrl = Url.Action("FilterMenuEmployees", "GridDemo"),
AllowFiltering = true,
AllowSorting = true,
PageCount = 1,
AllowPaging = true,
PageSize = 12,
FilterType = UiGridFilterType.Menu,
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "EmployeeID", HeaderText = "Employee ID", Type = UiColumnType.Number, Width = 150, TextAlign = UiTextAlign.Right, IsPrimaryKey = true },
new UiGridColumn { Field = "Employees", HeaderText = "Employee Name", Width = 200 },
new UiGridColumn { Field = "Designation", HeaderText = "Designation", Width = 180, TextAlign = UiTextAlign.Right },
new UiGridColumn { Field = "CurrentSalary", HeaderText = "CurrentSalary", Type = UiColumnType.Number, Width = 150, TextAlign = UiTextAlign.Right, Format = "C2" }
}
})
@Html.AntiForgeryToken()
@Html.UiGrid(new UiGridOptions
{
Id = "FilterMenu",
DataUrl = Url.Action("FilterMenuEmployees", "GridDemo"),
AllowFiltering = true,
AllowSorting = true,
PageCount = 1,
AllowPaging = true,
PageSize = 12,
FilterType = UiGridFilterType.Menu,
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "EmployeeID", HeaderText = "Employee ID", Type = UiColumnType.Number, Width = 150, TextAlign = UiTextAlign.Right, IsPrimaryKey = true },
new UiGridColumn { Field = "Employees", HeaderText = "Employee Name", Width = 200 },
new UiGridColumn { Field = "Designation", HeaderText = "Designation", Width = 180, TextAlign = UiTextAlign.Right },
new UiGridColumn { Field = "CurrentSalary", HeaderText = "CurrentSalary", Type = UiColumnType.Number, Width = 150, TextAlign = UiTextAlign.Right, Format = "C2" }
}
})
<div class="demo-property-panel">
<h3>Properties</h3>
<div class="demo-property-row"><label for="FilterType">Filter Type</label><select id="FilterType">@foreach (var option in (string[])ViewData["filterType"]) { <option value="@option">@option</option> }</select></div>
<div class="demo-property-row" id="infiniteRow" hidden><label for="dataloadtype">Enable OnDemand</label><input type="checkbox" id="dataloadtype" /></div>
<div class="demo-property-row" id="immediateRow" hidden><label for="immediateFilter">Enable Immediate Filter</label><input type="checkbox" id="immediateFilter" /></div>
</div>
<script>
document.getElementById('FilterType').value = 'Menu';
// Syncfusion: grid.filterSettings.type = the value. Excel and CheckBox also offer OnDemand (the values load as the list is scrolled) and Immediate (each tick filters at once).
function filterSettingsChanged() {
var type = document.getElementById('FilterType').value;
var popup = type === 'Excel' || type === 'CheckBox';
document.getElementById('infiniteRow').hidden = !popup;
document.getElementById('immediateRow').hidden = !popup;
window.Lumen.get('FilterMenu').setSettings({
filterType: type.toLowerCase(),
filterInfiniteScrolling: popup && document.getElementById('dataloadtype').checked,
filterMode: popup && document.getElementById('immediateFilter').checked ? 'immediate' : 'default'
});
}
document.getElementById('FilterType').addEventListener('change', function () {
document.getElementById('dataloadtype').checked = false;
document.getElementById('immediateFilter').checked = false;
filterSettingsChanged();
});
document.getElementById('dataloadtype').addEventListener('change', filterSettingsChanged);
document.getElementById('immediateFilter').addEventListener('change', filterSettingsChanged);
</script>
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class GridController : Controller
{
// The page is Views/Grid/FilterMenu.cshtml; it draws what ViewData holds.
public ActionResult FilterMenu()
{
ViewData["filterType"] = new string[] { "Menu", "Excel", "CheckBox" };
return View();
}
/// <summary>
/// The 10,000 employees the Filter Menu sample reads from its server action (Syncfusion's sample reads the same
/// count from its remote service): EmployeeID, Employees (the name), Designation and CurrentSalary.
/// </summary>
public class FilterMenuEmployee
{
private static List<FilterMenuEmployee> all;
public int EmployeeID { get; set; }
public string Employees { get; set; }
public string Designation { get; set; }
public double CurrentSalary { get; set; }
public static List<FilterMenuEmployee> GetAllRecords()
{
if (all == null)
{
string[] first = { "Anne", "Michael", "Margaret", "Janet", "Nancy", "Robert", "Steven", "Laura", "Andrew", "Albert", "Maria", "Frank", "Ann", "Paul", "Karin", "Hanna" };
string[] last = { "King", "Davolio", "Fuller", "Leverling", "Peacock", "Buchanan", "Suyama", "Callahan", "Dodsworth", "Hellstern", "Smith", "Wilson", "Taylor", "Brown", "Moore", "Clark" };
string[] titles = { "Sales Representative", "Vice President", "Sales Manager", "Inside Sales Coordinator", "Software Engineer", "Marketing Specialist", "Accountant", "Support Engineer" };
var list = new List<FilterMenuEmployee>();
for (int i = 0; i < 10000; i++)
{
list.Add(new FilterMenuEmployee
{
EmployeeID = 10001 + i,
Employees = first[i % first.Length] + " " + last[(i / first.Length) % last.Length],
Designation = titles[(i * 7) % titles.Length],
CurrentSalary = 30000 + (i * 137) % 70000
});
}
all = list;
}
return all;
}
}
/// <summary>The Filter Menu sample's 10,000 employees: UiDataOperations.Execute pages, sorts and filters them (and answers the filter popups' lists of values).</summary>
[HttpPost]
[ValidateJsonAntiForgeryToken]
public ActionResult FilterMenuEmployees(UiDataRequest request)
{
return Json(UiDataOperations.Execute(FilterMenuEmployee.GetAllRecords().AsQueryable(), request, "EmployeeID"));
}
}
}
@* Program.cs *@
builder.Services.AddControllersWithViews().AddLumenUIJson();
builder.Services.AddLumenUI();
// ...
app.UseLumenUI();
@* Views/_ViewImports.cshtml *@
@using Lumen.UI.Mvc
@addTagHelper *, Lumen.UI.Core
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@await RenderSectionAsync("scripts", required: false)
</body>
@* Grids with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@
The filtering feature allows users to display only the matching records based on filter criteria. To enable filtering, set the allowFiltering property to true. The Grid supports the following filter types:
MenuCheckBoxExcel
These can be configured using the filterSettings.type property. In this sample, the Menu filter is enabled by default, and you can switch to "CheckBox" or "Excel" filters using the dropdown. When using "CheckBox" or "Excel" filters, the Grid provides two enhancements:
- On-demand loading (Performance): Loads data only when needed, which improves speed and efficiency when working with large datasets. This can be enabled by setting the
filterSettings.enableInfiniteScrollingproperty totrue. - Immediate filtering (UI Experience): Applies filters instantly as soon as items are checked or unchecked, which provides a smoother interaction. This can be enabled by setting the
filterSettings.modeproperty toImmediate.
Injecting Module:
Features of the Grid component are organized into individual, feature-specific modules. To use filtering functionality, inject the Filter module by Grid.Inject(Filter).