Example of Infinite and Virtual Scrolling in ASP.NET Core Data Grid Control
Rows read in blocks while scrolling, or all rows in a virtual list.
No pager: the rows scroll in one list. Infinite scrolling (left) asks the server for the next
15 orders when you reach the end. Virtualization (right) has every order loaded
(ServerOperations = false) and only draws the rows in view.
@Html.AntiForgeryToken()
@Html.UiGrid(new UiGridOptions
{
Id = "infiniteGrid",
DataUrl = Url.Action("Orders", "GridDemo"),
EnableInfiniteScrolling = true, // Syncfusion: .EnableInfiniteScrolling()
PageSize = 15, // rows per block
Height = "320px",
AllowFiltering = true,
OnDataLoaded = "GridDemo.onDataLoaded",
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "OrderNo", HeaderText = "Order #", Width = 120 },
new UiGridColumn { Field = "Customer", HeaderText = "Customer Name", Width = 190 },
new UiGridColumn { Field = "Amount", Type = UiColumnType.Number, Format = "C2", Width = 130 }
}
})
@Html.UiGrid(new UiGridOptions
{
Id = "virtualGrid",
DataUrl = Url.Action("Orders", "GridDemo"),
ServerOperations = false,
EnableVirtualization = true, // Syncfusion: .EnableVirtualization()
Height = "320px",
AllowFiltering = true,
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "OrderNo", HeaderText = "Order #", Width = 120 },
new UiGridColumn { Field = "Customer", HeaderText = "Customer Name", Width = 190 },
new UiGridColumn { Field = "Amount", Type = UiColumnType.Number, Format = "C2", Width = 130 }
}
})
@Html.AntiForgeryToken()
<p>
No pager: the rows scroll in one list. <strong>Infinite scrolling</strong> (left) asks the server for the next
15 orders when you reach the end. <strong>Virtualization</strong> (right) has every order loaded
(<code>ServerOperations = false</code>) and only draws the rows in view.
</p>
<div class="row g-3">
<div class="col-lg-6">
@Html.UiGrid(new UiGridOptions
{
Id = "infiniteGrid",
DataUrl = Url.Action("Orders", "GridDemo"),
EnableInfiniteScrolling = true, // Syncfusion: .EnableInfiniteScrolling()
PageSize = 15, // rows per block
Height = "320px",
AllowFiltering = true,
OnDataLoaded = "GridDemo.onDataLoaded",
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "OrderNo", HeaderText = "Order #", Width = 120 },
new UiGridColumn { Field = "Customer", HeaderText = "Customer Name", Width = 190 },
new UiGridColumn { Field = "Amount", Type = UiColumnType.Number, Format = "C2", Width = 130 }
}
})
</div>
<div class="col-lg-6">
@Html.UiGrid(new UiGridOptions
{
Id = "virtualGrid",
DataUrl = Url.Action("Orders", "GridDemo"),
ServerOperations = false,
EnableVirtualization = true, // Syncfusion: .EnableVirtualization()
Height = "320px",
AllowFiltering = true,
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "OrderNo", HeaderText = "Order #", Width = 120 },
new UiGridColumn { Field = "Customer", HeaderText = "Customer Name", Width = 190 },
new UiGridColumn { Field = "Amount", Type = UiColumnType.Number, Format = "C2", Width = 130 }
}
})
</div>
</div>
<h3>Event log</h3>
<ul id="eventLog" style="font-family: monospace; font-size: 12px; max-height: 200px; overflow-y: auto; border: 1px solid var(--ui-border, #dadce3); padding: 8px;"></ul>
@section scripts {
<script>
window.GridDemo = (function () {
'use strict';
// Sample-local event-log utility.
function log(message) {
var list = document.getElementById('eventLog');
if (!list) { return; }
var item = document.createElement('li');
item.textContent = new Date().toLocaleTimeString() + ' - ' + message;
list.insertBefore(item, list.firstChild);
}
return {
onDataLoaded: function (rows, gridId) {
log(gridId + ': data loaded - ' + rows.length + ' row(s)');
}
};
})();
</script>
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
using Lumen.UI.Mvc.Demo.Models;
using Newtonsoft.Json;
// ServerOperations = true (default): the grid POSTs a UiDataRequest and expects { result, count } back (UiDataOperations.Execute).
// ServerOperations = false: return all rows once as a plain JSON array.
namespace Lumen.UI.Mvc.Demo.Controllers
{
public partial class GridDemoController : Controller
{
// SampleOrders.All is a shared in-memory list (also read by the chart demo); a real app would use its database instead.
private static readonly object Sync = SampleOrders.Sync;
[HttpPost]
[ValidateJsonAntiForgeryToken]
public ActionResult Orders(UiDataRequest request)
{
lock (Sync)
{
return Json(UiDataOperations.Execute(SampleOrders.All.AsQueryable(), request, "Id"));
}
}
}
}
namespace Lumen.UI.Mvc.Demo.Models
{
public class Order
{
public int Id { get; set; }
public string OrderNo { get; set; }
public DateTime OrderDate { get; set; }
public string Customer { get; set; }
/// <summary>Key of the customer (see SampleOrders.Customers), to show a foreign key column.</summary>
public int CustomerId { get; set; }
/// <summary>North, South, East or West: a column with few values, to show grouping and a dropdown editor.</summary>
public string Region { get; set; }
public decimal Amount { get; set; }
public bool Shipped { get; set; }
/// <summary>Long free text, to show text wrapping in the grid.</summary>
public string Notes { get; set; }
}
}
namespace Lumen.UI.Mvc.Demo.Models
{
/// <summary>In-memory test data for the grid and chart demos. A real app would query a database instead.</summary>
public static class SampleOrders
{
/// <summary>Lock held while reading or changing <see cref="All"/> (the grid demo edits it).</summary>
public static readonly object Sync = new object();
private static readonly string[] Customers =
{
"Santos Trading", "Reyes Hardware", "Dela Cruz Foods", "Bautista Auto Parts",
"Garcia Textiles", "Mendoza Electronics", "Torres Logistics", "Flores Bakery"
};
// Long notes to show text wrapping; some contain one very long word (a part number without spaces),
// which is broken inside the word only because it is wider than the column.
private static readonly string[] Notes =
{
"Deliver to the back entrance before 10 AM; the front gate is closed on weekends and public holidays.",
"Customer asked for partial delivery: send the available items now and the rest when back in stock.",
"Replacement part PN-XJ9000-ULTRAHEAVYDUTY-STAINLESSSTEELFLANGEASSEMBLY-REV7 must be checked before shipping.",
"Fragile.",
"Invoice must show the purchase order number, the project code and the name of the receiving warehouse.",
"Supercalifragilisticexpialidociousnessmaximizationprogramme discount applied at checkout.",
"Call the site supervisor thirty minutes before arrival so the loading bay can be cleared.",
"Standard delivery."
};
/// <summary>The values of Order.Region: the grouping demo's groups and the dropdown editor's choices.</summary>
public static readonly string[] Regions = { "North", "South", "East", "West" };
public static readonly List<Order> All = Build();
private static List<Order> Build()
{
// The demo builds made-up rows here (the fields are in the class above); a real app queries its database.
throw new NotImplementedException("Return your own rows.");
}
}
}
@* 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. *@