Example of Filter Bar Template in ASP.NET MVC Data Grid Control
This sample illustrates the Grid's filtering bar feature, utilizing custom components in the filter cells through the FilterBarTemplate feature. This functionality allows users to filter records based on specified criteria, displaying a reduced set of data. To enable filtering, set the AllowFiltering property to true , which renders a filter bar row next to the header. Users can then filter data by entering text into the cells of this row.
@Html.UiGrid(new UiGridOptions
{
Id = "FilterBarTemplate",
Data = ViewData["dataSource"],
GridLines = UiGridLines.Both,
AllowSorting = true,
AllowFiltering = true,
AllowPaging = true,
PageSize = 12,
PageCount = 5,
FilterType = UiGridFilterType.FilterBar,
ShowFilterBarOperator = true,
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "ProductID", HeaderText = "Product ID", Type = UiColumnType.Number, Width = 120, TextAlign = UiTextAlign.Right, IsPrimaryKey = true, FilterTemplate = new UiGridEditTemplate { Create = "numericCreate", Write = "numericWrite", Read = "numericRead" } },
new UiGridColumn { Field = "ProductName", HeaderText = "Product Name", Width = 220, FilterTemplate = new UiGridEditTemplate { Create = "stringCreate", Write = "stringWrite", Read = "stringRead" } },
new UiGridColumn { Field = "UnitPrice", HeaderText = "Price", Type = UiColumnType.Number, Width = 200, Format = "C2", TextAlign = UiTextAlign.Right, FilterTemplate = new UiGridEditTemplate { Create = "minMaxCreate", Write = "minMaxWrite", Read = "minMaxRead" } },
new UiGridColumn { Field = "UnitsInStock", HeaderText = "Stock", Type = UiColumnType.Number, Width = 120, Format = "N", TextAlign = UiTextAlign.Right },
new UiGridColumn { Field = "Discontinued", HeaderText = "Discontinued", Type = UiColumnType.Boolean, Width = 150, DisplayAsCheckBox = true, FilterTemplate = new UiGridEditTemplate { Create = "dropdownCreate", Write = "dropdownWrite", Read = "dropdownRead" } }
}
})
<style>
.e-cus-label { font-size: 14px; font-weight: 300; margin-bottom: 5px; text-align: start; }
.e-flex-layout { display: flex; gap: 8px; }
.e-flex-layout > div { flex: 1 1 0; min-width: 0; }
.e-min-max-separator { margin-right: 4px; }
.fbt-input { width: 100%; box-sizing: border-box; }
</style>
@Html.UiGrid(new UiGridOptions
{
Id = "FilterBarTemplate",
Data = ViewData["dataSource"],
GridLines = UiGridLines.Both,
AllowSorting = true,
AllowFiltering = true,
AllowPaging = true,
PageSize = 12,
PageCount = 5,
FilterType = UiGridFilterType.FilterBar,
ShowFilterBarOperator = true,
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "ProductID", HeaderText = "Product ID", Type = UiColumnType.Number, Width = 120, TextAlign = UiTextAlign.Right, IsPrimaryKey = true, FilterTemplate = new UiGridEditTemplate { Create = "numericCreate", Write = "numericWrite", Read = "numericRead" } },
new UiGridColumn { Field = "ProductName", HeaderText = "Product Name", Width = 220, FilterTemplate = new UiGridEditTemplate { Create = "stringCreate", Write = "stringWrite", Read = "stringRead" } },
new UiGridColumn { Field = "UnitPrice", HeaderText = "Price", Type = UiColumnType.Number, Width = 200, Format = "C2", TextAlign = UiTextAlign.Right, FilterTemplate = new UiGridEditTemplate { Create = "minMaxCreate", Write = "minMaxWrite", Read = "minMaxRead" } },
new UiGridColumn { Field = "UnitsInStock", HeaderText = "Stock", Type = UiColumnType.Number, Width = 120, Format = "N", TextAlign = UiTextAlign.Right },
new UiGridColumn { Field = "Discontinued", HeaderText = "Discontinued", Type = UiColumnType.Boolean, Width = 150, DisplayAsCheckBox = true, FilterTemplate = new UiGridEditTemplate { Create = "dropdownCreate", Write = "dropdownWrite", Read = "dropdownRead" } }
}
})
<script>
// Syncfusion's filter cells are widgets (NumericTextBox, TextBox, a pair for the price, a DropDownList) made in create / write functions; here the same functions make labelled inputs.
// Create builds the cell, Read gives the value the grid filters by (Enter or a change applies it).
var numElement, stringElement, minElement, maxElement, ddElement;
function labelled(text, input) {
var container = document.createElement('div');
var label = document.createElement('div');
label.className = 'e-cus-label';
label.textContent = text;
container.appendChild(label);
container.appendChild(input);
return container;
}
function field(type) {
var input = document.createElement('input');
input.type = type;
input.className = 'ui-grid-input fbt-input';
input.autocomplete = 'off';
return input;
}
function numericCreate() { numElement = field('number'); numElement.step = 'any'; return labelled('Id', numElement); }
function numericWrite() { }
function numericRead() { return numElement.value; }
function stringCreate() { stringElement = field('text'); return labelled('Name', stringElement); }
function stringWrite() { }
function stringRead() { return stringElement.value; }
function minMaxCreate() {
minElement = field('number');
maxElement = field('number');
minElement.step = maxElement.step = 'any';
var container = document.createElement('div');
container.className = 'e-flex-layout';
container.appendChild(labelled('Min', minElement));
container.appendChild(labelled('Max', maxElement));
// Syncfusion's keyPressed: Enter in either box filters the price between them (the grid's own single-value filter is not used here).
[minElement, maxElement].forEach(function (box) {
box.addEventListener('keydown', function (e) {
e.stopPropagation();
if (e.key === 'Enter') { filterPrice(); }
});
box.addEventListener('change', function (e) { e.stopPropagation(); filterPrice(); });
});
return container;
}
function minMaxWrite() { }
function minMaxRead() { return minElement.value; }
function filterPrice() {
var conditions = [];
if (minElement.value !== '') { conditions.push({ operator: 'greaterthanorequal', value: parseFloat(minElement.value) }); }
if (maxElement.value !== '') { conditions.push({ operator: 'lessthanorequal', value: parseFloat(maxElement.value) }); }
window.Lumen.get('FilterBarTemplate').filterByConditions('UnitPrice', conditions, 'and');
}
function dropdownCreate() {
ddElement = document.createElement('select');
ddElement.className = 'ui-grid-input fbt-input';
['Both', 'true', 'false'].forEach(function (v) {
var option = document.createElement('option');
option.value = v === 'Both' ? '' : v;
option.textContent = v;
ddElement.appendChild(option);
});
return labelled('Status', ddElement);
}
function dropdownWrite() { }
function dropdownRead() { return ddElement.value; }
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class GridController : Controller
{
// The page is Views/Grid/FilterBarTemplate.cshtml; it draws what ViewData holds.
public ActionResult FilterBarTemplate()
{
var product = Product.GetAllRecords();
ViewData["dataSource"] = product;
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>
@* Grids with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@
The FilterBarTemplate feature in the Data Grid allows customization of the controls in the filter bar. By default, a text box appears in the filter bar cell. In this demo, the Grid showcases various custom input components: a custom input component for the ID, Name and Price columns, and a Syncfusion DropDownList for the Discontinued column, all achieved through the filter bar template feature. You can customize the filter components in the filter cells by setting the FilterBarTemplate property for each column. The Unit Stock column uses the default filter bar cell with operator functionality.