Example of Grouping with Paging in ASP.NET MVC Data Grid Control
This sample demonstrates the lazy load grouping feature of the Grid component.
@* 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 = "LazyGroupGrid",
DataUrl = Url.Action("LazyCustomers", "GridDemo"),
AllowGrouping = true,
AllowSorting = true,
Height = "400px",
AllowPaging = true,
PageSize = 12,
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 = "LazyGroupGrid",
DataUrl = Url.Action("LazyCustomers", "GridDemo"),
AllowGrouping = true,
AllowSorting = true,
Height = "400px",
AllowPaging = true,
PageSize = 12,
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/LazyPaging.cshtml; it draws what ViewData holds.
public ActionResult LazyPaging()
{
// 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"));
}
}
}
@* 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 Lazy load grouping, allows the Grid to render only the initial level caption rows in the collapsed state while grouping. The child rows of each caption will render only when we expand the captions. To enable lazy load grouping, set groupSettings->enableLazyLoading property as true.