Your search did not match any results.
Tree View

Load Data on Demand


In those special cases when the built-in Virtual Mode fails, you can gain full control over data loading in the createChildren function.This function is called each time a user expands the TreeView node that has not been expanded before. In the createChildren function, you get data from a server or other sources and then form the collection of child nodes from this data.

@(Html.DevExtreme().TreeView() .ID("simple-treeview") .CreateChildren("treeView_createChildren") .RootValue("") .DataStructure(TreeViewDataStructure.Plain) .Height(500) ) <script> function treeView_createChildren(parent) { var parentId = parent ? : ""; return $.ajax({ url: "@Url.Content("~/api/TreeViewData")", dataType: "json", data: { parentId: parentId } }); } </script>
using DevExtreme.AspNet.Data; using DevExtreme.AspNet.Mvc; using DevExtreme.MVC.Demos.Models; using DevExtreme.MVC.Demos.Models.SampleData; using Newtonsoft.Json; using System.Collections.Generic; using System.Linq; using System.Web.Mvc; namespace DevExtreme.MVC.Demos.Controllers { public class TreeViewController : Controller { public ActionResult LoadDataOnDemand() { return View(); } } }
using System; using System.Linq; using System.Net.Http; using System.Web.Http; using System.Web.Hosting; using System.IO; namespace DevExtreme.MVC.Demos.Controllers.ApiControllers { public class TreeViewDataController : ApiController { [HttpGet] public HttpResponseMessage Get(string parentId) { var rootPath = HostingEnvironment.MapPath("~"); var parentPath = String.IsNullOrEmpty(parentId) ? rootPath : Path.Combine(rootPath, parentId); var childNodes = Directory.EnumerateFileSystemEntries(parentPath) .Where(path => Path.GetFullPath(path).StartsWith(rootPath)) .Select(path => new { id = Path.Combine(parentPath, Path.GetFileName(path)), parentId, text = Path.GetFileName(path), hasItems = File.GetAttributes(path).HasFlag(FileAttributes.Directory) }) .Where(i => i.text != "bin" && i.text != "obj" && !i.text.StartsWith(".")) .OrderByDescending(i => i.hasItems) .ThenBy(i => i.text); return Request.CreateResponse(childNodes); } } }