Example of Grouping with Virtual Scrolling in ASP.NET Core Data Grid Control
This sample demonstrates the lazy loading grouping feature with virtual scrolling.
@* The grid's settings are in the view; the data they read (ViewData) is set in the controller action (see the controller tab). *@
@Html.AntiForgeryToken()
@Html.UiGrid(new UiGridOptions
{
Id = "Grid",
DataUrl = Url.Action("LazyCustomers", "GridDemo"),
Height = "400px",
EnableVirtualization = true,
AllowGrouping = true,
AllowSorting = true,
GroupSettings = new UiGroupSettings { EnableLazyLoading = true, Columns = new[] { "ProductName", "CustomerName" } },
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "OrderID", HeaderText = "Order ID", AllowGrouping = false, Width = 120, TextAlign = UiTextAlign.Right },
new UiGridColumn { Field = "ProductName", HeaderText = "Product Name", Width = 160 },
new UiGridColumn { Field = "ProductID", HeaderText = "Product ID", Width = 120, TextAlign = UiTextAlign.Right },
new UiGridColumn { Field = "CustomerID", HeaderText = "Customer ID", Width = 120 },
new UiGridColumn { Field = "CustomerName", HeaderText = "Customer Name", Width = 160 }
}
})
@Html.AntiForgeryToken()
@Html.UiGrid(new UiGridOptions
{
Id = "Grid",
DataUrl = Url.Action("LazyCustomers", "GridDemo"),
Height = "400px",
EnableVirtualization = true,
AllowGrouping = true,
AllowSorting = true,
GroupSettings = new UiGroupSettings { EnableLazyLoading = true, Columns = new[] { "ProductName", "CustomerName" } },
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "OrderID", HeaderText = "Order ID", AllowGrouping = false, Width = 120, TextAlign = UiTextAlign.Right },
new UiGridColumn { Field = "ProductName", HeaderText = "Product Name", Width = 160 },
new UiGridColumn { Field = "ProductID", HeaderText = "Product ID", Width = 120, TextAlign = UiTextAlign.Right },
new UiGridColumn { Field = "CustomerID", HeaderText = "Customer ID", Width = 120 },
new UiGridColumn { Field = "CustomerName", HeaderText = "Customer Name", Width = 160 }
}
})
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/LazyVirtual.cshtml; it draws what ViewData holds.
public ActionResult LazyVirtual()
{
// the grid reads the group captions, then the rows of a group, from the LazyCustomers action of the controller
return View();
}
public class LazyCustomer
{
private static List<LazyCustomer> all;
public int OrderID { get; set; }
public string CustomerID { get; set; }
public string CustomerName { get; set; }
public string CustomerAddress { get; set; }
public string ProductName { get; set; }
public int ProductID { get; set; }
public string Quantity { get; set; }
/// <summary>20,000 orders made from short lists of customers, products and addresses (Syncfusion's sample makes its 20,000 the same way).</summary>
public static List<LazyCustomer> All
{
get
{
if (all == null)
{
string[] customerId = { "VINET", "TOMSP", "HANAR", "VICTE", "SUPRD", "CHOPS", "RICSU", "WELLI", "HILAA", "ERNSH", "CENTC", "OTTIK", "QUEDE", "RATTC", "FOLKO", "BLONP", "WARTH", "FRANK", "GROSR", "WHITC" };
string[] customerName = { "Maria", "Ana Trujillo", "Antonio Moreno", "Thomas Hardy", "Christina Berglund", "Hanna Moos", "Frederique Citeaux", "Martin Sommer", "Laurence Lebihan", "Elizabeth Lincoln", "Victoria Ashworth", "Patricio Simpson", "Francisco Chang", "Yang Wang", "Pedro Afonso", "Elizabeth Brown", "Sven Ottlieb", "Janine Labrune", "Ann Devon", "Roland Mendel" };
string[] address = { "507 - 20th Ave. E. Apt. 2A", "908 W. Capital Way", "722 Moss Bay Blvd.", "4110 Old Redmond Rd.", "14 Garrett Hill", "4726 - 11th Ave. N.E.", "7 Houndstooth Rd.", "59 rue de l'Abbaye", "Luisenstr. 48", "2817 Milton Dr." };
string[] product = { "Chai", "Chang", "Aniseed Syrup", "Chef Anton's Cajun Seasoning", "Chef Anton's Gumbo Mix", "Grandma's Boysenberry Spread", "Northwoods Cranberry Sauce", "Mishi Kobe Niku", "Ikura", "Queso Cabrales", "Queso Manchego La Pastora", "Tofu", "Genen Shouyu", "Pavlova", "Alice Mutton", "Carnarvon Tigers", "Sasquatch Ale", "Steeleye Stout", "Outback Lager", "Chocolade" };
string[] quantity = { "10 boxes x 20 bags", "24 - 12 oz bottles", "12 - 550 ml bottles", "48 - 6 oz jars", "36 boxes", "12 - 8 oz jars", "1 kg pkg.", "10 - 500 g pkgs.", "2 kg box", "40 - 100 g pkgs." };
var list = new List<LazyCustomer>();
for (int x = 0; x < 20000; x++)
{
list.Add(new LazyCustomer
{
OrderID = 10248 + x,
CustomerID = customerId[x % customerId.Length],
CustomerName = customerName[(x * 7) % customerName.Length],
CustomerAddress = address[x % address.Length],
ProductName = product[(x * 3) % product.Length],
ProductID = 1 + (x * 3) % product.Length,
Quantity = quantity[x % quantity.Length]
});
}
all = list;
}
return all;
}
}
}
/// <summary>The 20,000 orders of the lazy-load grouping samples: UiDataOperations.Execute answers the group captions first, then the rows of a group as it is opened.</summary>
[HttpPost]
[ValidateJsonAntiForgeryToken]
public ActionResult LazyCustomers(UiDataRequest request)
{
return Json(UiDataOperations.Execute(LazyCustomer.All.AsQueryable(), request, "OrderID"));
}
}
}
@* 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 lazy load grouping allows the grid to render only the initial level caption rows in the collapsed state while grouping. Now, the Data Grid supports lazy load grouping for virtual scrolling enabled grid too, which means the records of each group caption will render only when you expand the group captions row. Buffer data is loaded while scrolling vertically or horizontally. This is done by setting the GroupSettings->EnableLazyLoading property as true and the EnableVirtualization property as true.
Note: The Height property must be defined when enabling the EnableVirtualization.