TreeView
Lazy Loading
This view shows how to load the child nodes dynamically in TreeView.
Features
Description
Lazy loading is useful when you are dealing with large hierarchical data sources and would like to avoid the delays involved in loading the entire data set at once.
The TreeView control makes lazy-loading super easy. Only set the second parameter lazyLoadActionUrl and provide a url to obtain the lazy nodes data. If the child node has children, you can set an empty array to the field which name is what the ChildItemsPath property stands for. Otherwise, let it to be null.
If you wants to use lazy-loading with javascript, you can set the TreeView's OnClientLazyLoadFunction property to a function to be called when the user expands the node. This function takes two parameters: the parent node and a callback function to be invoked when the data becomes available.
The tree in example below starts with three lazy-loaded nodes. When you expand them, the OnClientLazyLoadFunction is invoked. The function uses a setTimeout to simulate an http delay and returns data for three child nodes, one of which is also a lazy-loaded node.
The example also uses some CSS to animate the node icons while they are being loaded.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | using C1.Web.Mvc; using C1.Web.Mvc.Serialization; using MvcExplorer.Models; using System.Web.Mvc; namespace MvcExplorer.Controllers { partial class TreeViewController : Controller { // GET: LazyLoad public ActionResult LazyLoading() { return View(); } public ActionResult Load() { return this .C1Json(EmployeeEx.GetEmployees( null ), useCamelCasePropertyName: false ); } public ActionResult LazyLoading_LoadAction([C1JsonRequest]TreeNode node) { var leaderID = ( int ?)node.DataItem[ "EmployeeID" ]; return this .C1Json(EmployeeEx.GetEmployees(leaderID), useCamelCasePropertyName: false ); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @ (Html.C1().TreeView() .Bind(Url.Action( "Load" ), Url.Action( "LazyLoading_LoadAction" )) .DisplayMemberPath( "Name" ) .ChildItemsPath( "SubExployees" )) @section Summary{ < p > @Html .Raw(Resources.TreeView.LazyLoading_Text0)</ p > } @section Description{ < p > @Html .Raw(Resources.TreeView.LazyLoading_Text1)</ p > < p > @Html .Raw(Resources.TreeView.LazyLoading_Text2)</ p > < p > @Html .Raw(Resources.TreeView.LazyLoading_Text3)</ p > < p > @Html .Raw(Resources.TreeView.LazyLoading_Text4)</ p > < p > @Html .Raw(Resources.TreeView.LazyLoading_Text5)</ p > } |