Example of Paging (API) in ASP.NET Core Data Grid Control
This sample demonstrates the usage of paging API in Grid. In this sample, use the properties panel to change the page size, page count and current page of the Grid.
Properties
@Html.UiGrid(new UiGridOptions
{
Id = "Grid",
Data = ViewData["dataSource"],
PageCount = 2,
AllowSorting = true,
AllowFiltering = true,
AllowPaging = true,
PageSize = 12,
FilterType = UiGridFilterType.Excel,
EditSettings = new UiEditSettings { AllowAdding = true, AllowEditing = true, AllowDeleting = true },
Toolbar = new List<string> { "Add", "Edit", "Delete", "Update", "Cancel" },
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "ProductID", HeaderText = "Product ID", Type = UiColumnType.Number, Width = 130, TextAlign = UiTextAlign.Right, IsPrimaryKey = true, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "ProductName", HeaderText = "Product Name", Width = 190, ValidationRules = new UiValidationRules { Required = true, MinLength = 5 } },
new UiGridColumn { Field = "UnitPrice", HeaderText = "Unit Price", Type = UiColumnType.Number, Width = 135, TextAlign = UiTextAlign.Right, Format = "C2", EditType = UiEditType.NumericEdit },
new UiGridColumn { Field = "UnitsInStock", HeaderText = "Units In Stock", Type = UiColumnType.Number, Width = 160, TextAlign = UiTextAlign.Right }
}
})
<style>
.e-pager .e-pagecountmsg {
display: inline-block;
}
</style>
@Html.UiGrid(new UiGridOptions
{
Id = "Grid",
Data = ViewData["dataSource"],
PageCount = 2,
AllowSorting = true,
AllowFiltering = true,
AllowPaging = true,
PageSize = 12,
FilterType = UiGridFilterType.Excel,
EditSettings = new UiEditSettings { AllowAdding = true, AllowEditing = true, AllowDeleting = true },
Toolbar = new List<string> { "Add", "Edit", "Delete", "Update", "Cancel" },
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "ProductID", HeaderText = "Product ID", Type = UiColumnType.Number, Width = 130, TextAlign = UiTextAlign.Right, IsPrimaryKey = true, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "ProductName", HeaderText = "Product Name", Width = 190, ValidationRules = new UiValidationRules { Required = true, MinLength = 5 } },
new UiGridColumn { Field = "UnitPrice", HeaderText = "Unit Price", Type = UiColumnType.Number, Width = 135, TextAlign = UiTextAlign.Right, Format = "C2", EditType = UiEditType.NumericEdit },
new UiGridColumn { Field = "UnitsInStock", HeaderText = "Units In Stock", Type = UiColumnType.Number, Width = 160, TextAlign = UiTextAlign.Right }
}
})
<div class="demo-property-panel">
<h3>Properties</h3>
<div class="demo-property-row"><label for="pageSize">Page Size</label><input type="number" id="pageSize" min="1" max="77" value="12" /></div>
<div class="demo-property-row"><label for="pageCount">Page Count</label><input type="number" id="pageCount" min="1" max="7" value="2" /></div>
<div class="demo-property-row"><label for="currentPage">Current Page</label><input type="number" id="currentPage" min="1" max="7" value="1" /></div>
</div>
<script>
var pagingGrid = function () { return window.Lumen.get('Grid'); };
// Syncfusion: grid.pageSettings.pageSize / pageCount = the value; grid.goToPage(n).
document.getElementById('pageSize').addEventListener('change', function (e) {
var size = Math.max(1, Math.min(77, parseInt(e.target.value, 10) || 12));
e.target.value = size;
pagingGrid().setPageSize(size);
document.getElementById('currentPage').max = Math.ceil(pagingGrid().getTotalRecords() / size);
});
document.getElementById('pageCount').addEventListener('change', function (e) {
var count = Math.max(1, Math.min(7, parseInt(e.target.value, 10) || 2));
e.target.value = count;
pagingGrid().setSettings({ pageCount: count });
});
document.getElementById('currentPage').addEventListener('change', function (e) {
var page = Math.max(1, Math.min(parseInt(e.target.max, 10) || 1, parseInt(e.target.value, 10) || 1));
e.target.value = page;
pagingGrid().goToPage(page);
});
</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/PagingApi.cshtml; it draws what ViewData holds.
public ActionResult PagingApi()
{
var DataSource = Product.GetAllRecords();
ViewData["dataSource"] = DataSource;
return View();
}
}
}
@* 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. *@
Paging allows you to display the contents of the Grid in page segments. The number of items on a page is determined by the PageSettings->PageSize property. If no value is specified for the PageSettings->PageSize property, the Grid will display 12 items on a page. By default, paging is disabled. To enable paging, set AllowPaging property to true.
In this demo,
- Click the Allow Paging check box to enable/disable paging feature.
- Change the value of Page Size textbox to change
PageSettings->PageSize. - Change the value of Page Count textbox to change
PageSettings->PageCount. - Change the value of Current Page textbox to change
PageSettings->CurrentPage. - Changed default pager details information using the
totalItemsInfolocale property.