Example of Virtualization in ASP.NET MVC TreeView Control
This example demonstrates virtualization support in ASP.NET MVC TreeView. The component has 8000 items bound to it including children; however, when you open the suggestion list, only few items are loaded based on the height specified, and the remaining items are loaded while scrolling.
@Html.UiTreeView(new UiTreeViewOptions { Id = "tree", Height = "400px", EnableVirtualization = true, ShowCheckBox = true, Nodes = (List<UiTreeNode>)ViewData["virtualFields"] })
<style>
.control_wrapper { max-width: 700px; margin: auto; border: 1px solid #dddddd; border-radius: 3px; }
</style>
<div class="control-section">
<div class="control_wrapper">
@Html.UiTreeView(new UiTreeViewOptions { Id = "tree", Height = "400px", EnableVirtualization = true, ShowCheckBox = true, Nodes = (List<UiTreeNode>)ViewData["virtualFields"] })
</div>
</div>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class TreeViewController : Controller
{
// The page is Views/TreeView/Virtualization.cshtml; it draws what ViewData holds.
public ActionResult Virtualization()
{
// Syncfusion: 8000 nodes, the departments of an organization with their employees
string[] departments = { "Engineering", "Sales", "Human Resources", "Finance", "Marketing", "Customer Support", "Operations", "Legal", "Research", "IT Infrastructure" };
string[] roles = { "Manager", "Analyst", "Specialist", "Coordinator", "Lead", "Associate", "Consultant", "Administrator", "Executive", "Supervisor" };
List<UiTreeNode> organization = new List<UiTreeNode>();
int made = 0;
for (int d = 0; made < 8000; d++)
{
UiTreeNode department = new UiTreeNode { Id = "d" + d, Text = departments[d % departments.Length] + (d >= departments.Length ? " " + (d / departments.Length + 1) : string.Empty), Expanded = d == 0 };
made++;
for (int e = 1; e <= 20 && made < 8000; e++, made++)
{
department.Children.Add(new UiTreeNode { Id = "d" + d + "-" + e, Text = roles[(d + e) % roles.Length] + " " + e });
}
organization.Add(department);
}
ViewData["virtualFields"] = organization;
return View();
}
}
}
@* 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>
The TreeView component supports UI virtualization to improve rendering performance when handling a large amount of data. Enable this feature by setting the enableVirtualization property to true and providing a fixed height for the TreeView container.
In this demo, a large organization-like dataset is generated programmatically and virtualization is enabled to render the nodes efficiently.
For more details, refer to the Virtualization documentation.