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() .CreateChildren("treeView_createChildren") .RootValue("") .DataStructure(TreeViewDataStructure.Plain) .Height(500) ) <script> function treeView_createChildren(parent) { var parentId = parent ? : ""; return $.ajax({ url: "@Url.Content("~/api/TreeViewData/Get")", data: { parentId: parentId } }); } </script>
using DevExtreme.AspNet.Data; using DevExtreme.AspNet.Mvc; using DevExtreme.NETCore.Demos.Models; using DevExtreme.NETCore.Demos.Models.SampleData; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using System.Collections.Generic; namespace DevExtreme.NETCore.Demos.Controllers { public class TreeViewController : Controller { public ActionResult LoadDataOnDemand() { return View(); } } }
using System; using System.Linq; using System.Net.Http; using System.IO; using Microsoft.AspNetCore.Hosting.Internal; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Hosting; namespace DevExtreme.NETCore.Demos.Controllers.ApiControllers { [Route("api/[controller]/[action]")] public class TreeViewDataController : Controller { IHostingEnvironment _hostingEnvironment; public TreeViewDataController(IHostingEnvironment hostingEnvironment) { _hostingEnvironment = hostingEnvironment; } [HttpGet] public object Get(string parentId) { var rootPath = _hostingEnvironment.ContentRootPath; 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 = parentId, text = Path.GetFileName(path), hasItems = System.IO.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 childNodes; } } }