Example of Default Filtering in ASP.NET Core Data Grid Control
This sample demonstrates the Grid's default filtering feature. Type a value in the filterbar and press enter to filter a particular column.
Properties
@Html.UiGrid(new UiGridOptions
{
Id = "DefaultFiltering",
Data = ViewData["dataSource"],
AllowFiltering = true,
AllowSorting = true,
PageCount = 2,
AllowPaging = true,
PageSize = 12,
FilterType = UiGridFilterType.FilterBar,
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "CategoryName", HeaderText = "Category Name", Width = 150 },
new UiGridColumn { Field = "ProductName", HeaderText = "Product Name", Width = 150 },
new UiGridColumn { Field = "UnitsInStock", HeaderText = "Units In Stock", Type = UiColumnType.Number, Width = 150, TextAlign = UiTextAlign.Right },
new UiGridColumn { Field = "Discontinued", HeaderText = "Discontinued", Type = UiColumnType.Boolean, Width = 150, TextAlign = UiTextAlign.Center, DisplayAsCheckBox = true }
}
})
@Html.UiGrid(new UiGridOptions
{
Id = "DefaultFiltering",
Data = ViewData["dataSource"],
AllowFiltering = true,
AllowSorting = true,
PageCount = 2,
AllowPaging = true,
PageSize = 12,
FilterType = UiGridFilterType.FilterBar,
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "CategoryName", HeaderText = "Category Name", Width = 150 },
new UiGridColumn { Field = "ProductName", HeaderText = "Product Name", Width = 150 },
new UiGridColumn { Field = "UnitsInStock", HeaderText = "Units In Stock", Type = UiColumnType.Number, Width = 150, TextAlign = UiTextAlign.Right },
new UiGridColumn { Field = "Discontinued", HeaderText = "Discontinued", Type = UiColumnType.Boolean, Width = 150, TextAlign = UiTextAlign.Center, DisplayAsCheckBox = true }
}
})
<div class="demo-property-panel">
<h3>Properties</h3>
<div class="demo-property-row"><label for="checked">Enable Filterbar Operator</label><input type="checkbox" id="checked" /></div>
</div>
<script>
document.getElementById('checked').addEventListener('change', function (e) {
// Syncfusion: grid.filterSettings.showFilterBarOperator = the value; the grid is drawn again with it.
window.Lumen.get('DefaultFiltering').setSettings({ showFilterBarOperator: e.target.checked });
});
</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/DefaultFiltering.cshtml; it draws what ViewData holds.
public ActionResult DefaultFiltering()
{
var product = Categories.GetAllRecords();
ViewData["datasource"] = product;
return View();
}
public class Categories
{
private bool? _discont = false;
public Categories()
{
}
public Categories(string CategoryName, string ProductName, string QuantityPerUnit, bool Discontinued, short UnitsInStock)
{
this.CategoryName = CategoryName;
this.ProductName = ProductName;
this.QuantityPerUnit = QuantityPerUnit;
this.UnitsInStock = UnitsInStock;
this.Discontinued = Discontinued;
}
public static List<Categories> GetAllRecords()
{
List<Categories> category = new List<Categories>();
category.Add(new Categories("Beverages", "Chai", "10 boxes x 20 bags", true, 39));
category.Add(new Categories("Beverages", "Chang", "24 - 12 oz bottles", true, 17));
category.Add(new Categories("Beverages", "Chartreuse verte", "750 cc per bottle", true, 69));
category.Add(new Categories("Beverages", "Cte de Blaye", "12 - 75 cl bottles", false, 17));
category.Add(new Categories("Beverages", "Ipoh Coffee", "16 - 500 g tins", true, 17));
category.Add(new Categories("Beverages", "Lakkalikri", "500 ml", true, 57));
category.Add(new Categories("Beverages", "Laughing Lumberjack Lager", "24 - 12 oz bottles", false, 52));
category.Add(new Categories("Beverages", "Outback Lager", "24 - 355 ml bottles", false, 15));
category.Add(new Categories("Beverages", "Rhnbru Klosterbier", "24 - 0.5 l bottles", false, 125));
category.Add(new Categories("Beverages", "Sasquatch Ale", "24 - 12 oz bottles", true, 111));
category.Add(new Categories("Beverages", "Steeleye Stout", "24 - 12 oz bottles", true, 20));
category.Add(new Categories("Condiments", "Aniseed Syrup", "12 - 550 ml bottles", true, 13));
category.Add(new Categories("Condiments", "Chef Anton's Cajun Seasoning", "48 - 6 oz jars", false, 53));
category.Add(new Categories("Condiments", "Genen Shouyu", "24 - 250 ml bottles", false, 39));
category.Add(new Categories("Condiments", "Grandma's Boysenberry Spread", "12 - 8 oz jars", false, 120));
category.Add(new Categories("Condiments", "Gula Malacca", "20 - 2 kg bags", true, 27));
category.Add(new Categories("Condiments", "Louisiana Fiery Hot Pepper Sauce", "32 - 8 oz bottles", false, 76));
category.Add(new Categories("Condiments", "Louisiana Hot Spiced Okra", "24 - 8 oz jars", false, 4));
category.Add(new Categories("Condiments", "Northwoods Cranberry Sauce", "12 - 12 oz jars", true, 6));
category.Add(new Categories("Condiments", "Original Frankfurter grne Soe", "12 boxes", false, 32));
category.Add(new Categories("Condiments", "Sirop d'rable", "24 - 500 ml bottles", false, 113));
category.Add(new Categories("Condiments", "Vegie-spread", "15 - 625 g jars", false, 24));
category.Add(new Categories("Confections", "Chocolade", "10 pkgs.", true, 15));
category.Add(new Categories("Confections", "Gumbr Gummibrchen", "100 - 250 g bags", false, 15));
category.Add(new Categories("Confections", "Maxilaku", "24 - 50 g pkgs.", false, 10));
category.Add(new Categories("Confections", "NuNuCa Nu-Nougat-Creme", "20 - 450 g glasses", true, 76));
category.Add(new Categories("Confections", "Pavlova", "32 - 500 g boxes", true, 29));
category.Add(new Categories("Confections", "Schoggi Schokolade", "100 - 100 g pieces", true, 49));
category.Add(new Categories("Confections", "Scottish Longbreads", "10 boxes x 8 pieces", true, 6));
category.Add(new Categories("Confections", "Sir Rodney's Marmalade", "30 gift boxes", true, 40));
category.Add(new Categories("Confections", "Sir Rodney's Scones", "24 pkgs. x 4 pieces", true, 3));
category.Add(new Categories("Confections", "Tarte au sucre", "48 pies", false, 17));
category.Add(new Categories("Confections", "Teatime Chocolate Biscuits", "10 boxes x 12 pieces", false, 25));
category.Add(new Categories("Confections", "Valkoinen suklaa", "12 - 100 g bars", false, 65));
category.Add(new Categories("Confections", "Zaanse koeken", "10 - 4 oz boxes", true, 36));
category.Add(new Categories("Dairy Categorys", "Camembert Pierrot", "15 - 300 g rounds", false, 19));
category.Add(new Categories("Dairy Categorys", "Flotemysost", "10 - 500 g pkgs.", false, 26));
category.Add(new Categories("Dairy Categorys", "Geitost", "500 g", false, 112));
category.Add(new Categories("Dairy Categorys", "Gorgonzola Telino", "12 - 100 g pkgs", true, 0));
category.Add(new Categories("Dairy Categorys", "Gudbrandsdalsost", "10 kg pkg.", true, 26));
category.Add(new Categories("Dairy Categorys", "Mascarpone Fabioli", "24 - 200 g pkgs.", true, 9));
category.Add(new Categories("Dairy Categorys", "Mozzarella di Giovanni", "24 - 200 g pkgs.", true, 14));
category.Add(new Categories("Dairy Categorys", "Queso Cabrales", "1 kg pkg.", true, 22));
category.Add(new Categories("Dairy Categorys", "Queso Manchego La Pastora", "10 - 500 g pkgs.", true, 86));
category.Add(new Categories("Dairy Categorys", "Raclette Courdavault", "5 kg pkg.", false, 79));
category.Add(new Categories("Grains/Cereals", "Filo Mix", "16 - 2 kg boxes", false, 38));
category.Add(new Categories("Grains/Cereals", "Gnocchi di nonna Alice", "24 - 250 g pkgs.", false, 21));
category.Add(new Categories("Grains/Cereals", "Gustaf's Knckebrd", "24 - 500 g pkgs.", true, 104));
category.Add(new Categories("Grains/Cereals", "Ravioli Angelo", "24 - 250 g pkgs.", true, 36));
category.Add(new Categories("Grains/Cereals", "Tunnbrd", "12 - 250 g pkgs.", true, 61));
category.Add(new Categories("Grains/Cereals", "Wimmers gute Semmelkndel", "20 bags x 4 pieces", true, 22));
category.Add(new Categories("Meat/Poultry", "Pt chinois", "24 boxes x 2 pies", false, 115));
category.Add(new Categories("Meat/Poultry", "Tourtire", "16 pies", false, 21));
category.Add(new Categories("Produce", "Longlife Tofu", "5 kg pkg.", false, 4));
category.Add(new Categories("Produce", "Manjimup Dried Apples", "50 - 300 g pkgs.", false, 20));
category.Add(new Categories("Produce", "Tofu", "40 - 100 g pkgs.", true, 35));
category.Add(new Categories("Produce", "Uncle Bob's Organic Dried Pears", "12 - 1 lb pkgs.", true, 15));
category.Add(new Categories("Seafood", "Boston Crab Meat", "24 - 4 oz tins", true, 123));
category.Add(new Categories("Seafood", "Carnarvon Tigers", "16 kg pkg.", true, 42));
category.Add(new Categories("Seafood", "Escargots de Bourgogne", "24 pieces", true, 62));
category.Add(new Categories("Seafood", "Gravad lax", "12 - 500 g pkgs.", true, 11));
category.Add(new Categories("Seafood", "Ikura", "12 - 200 ml jars", false, 31));
category.Add(new Categories("Seafood", "Inlagd Sill", "24 - 250 g jars", false, 112));
category.Add(new Categories("Seafood", "Jack's New England Clam Chowder", "12 - 12 oz cans", false, 85));
category.Add(new Categories("Seafood", "Konbu", "2 kg box", false, 24));
category.Add(new Categories("Seafood", "Nord-Ost Matjeshering", "10 - 200 g glasses", false, 10));
category.Add(new Categories("Seafood", "Rd Kaviar", "24 - 150 g jars", false, 101));
category.Add(new Categories("Seafood", "Rogede sild", "1k pkg.", false, 5));
category.Add(new Categories("Seafood", "Spegesild", "4 - 450 g glasses", true, 95));
return category;
}
public string CategoryName { get; set; }
public string ProductName { get; set; }
public string QuantityPerUnit { get; set; }
public short UnitsInStock { get; set; }
public bool? Discontinued {
get
{
return this._discont;
}
set
{
this._discont = value;
}
}
}
}
}
@* 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 enables the user to view a reduced amount of records based on filter criteria. It can be enabled by setting the AllowFiltering property to true. A filter bar row will be rendered next to header which allows users to filter data by entering text within its cells.
The Filterbar uses two modes which specifies how to start filtering. They are,
OnEnter- Enabled by default, filter will be initiated when theEnterkey is pressed.-
Immediate- Filter will start after user finishes typing. There will be a time delay of 1500ms to initiate filter after the user stops typing. It can be overridden using theFilterSettings->ImmediateModeDelayproperty.
In this demo, you can type the text in the filter bar cells to filter.
Additionally, the records can also be filtered based on the selected filterbar operator. It can be enabled by setting
filterSettings->showFilterBarOperator property to true.
In this demo,
- To enable or disable filterbar operator feature, check or uncheck the checkbox in the properties panel.
- Select the required filtering operator in the dropdown list on the filter bar cell and type the text to start filtering.
- Now, the addition of new filter operators such as "Does Not Contain", "Does Not End With", "Does Not Start With", "Empty", "Not Empty", "Null", "Not Null", "Like", and "Wildcard search" greatly enhance the filtering feature of the Grid.
For example, when the Like search operator is used:
- %a% - Filters words containing the character 'a'
- a% - Filters words ending with 'a'
- %a - Filters words starting with 'a'
For example when the Wildcard search operator is used:
- a*b - Filters words that start with 'a' and end with 'b'