The TreeView provides support for declaratively defining its items and for local (on the server) and remote (using a DataSource configuration object) binding.
<kendo-treeviewauto-bind="true"load-on-demand="true"name="treeview"><items><treeview-itemexpanded="true"text="My Web Site"><items><treeview-itemexpanded="true"text="images"><items><treeview-itemtext="logo.png"></treeview-item><treeview-itemtext="body-back.png"></treeview-item></items></treeview-item><treeview-itemtext="about.html"></treeview-item><treeview-itemtext="contacts.html"></treeview-item></items></treeview-item></items></kendo-treeview>
publicActionResultLocal_Data_Binding(){
ViewBag.treeData =GetData();returnView();}privateIEnumerable<TreeViewItemModel>GetData(){List<TreeViewItemModel> inline =newList<TreeViewItemModel>{newTreeViewItemModel{
Text ="Furniture",
Items =newList<TreeViewItemModel>{newTreeViewItemModel(){
Text ="Tables & Chairs"},newTreeViewItemModel{
Text ="Sofas"},newTreeViewItemModel{
Text ="Occasional Furniture"}}},newTreeViewItemModel{
Text ="Decor",
Items =newList<TreeViewItemModel>{newTreeViewItemModel(){
Text ="Bed Linen"},newTreeViewItemModel{
Text ="Curtains & Blinds"},newTreeViewItemModel{
Text ="Carpets"}}}};return inline;}
TreeViewItemModel
public class TreeViewItemViewModel
{
public string Id { get; set; }
public bool Expanded { get; set; }
public bool Encoded { get; set; }
public string Text { get; set; }
public string SpriteCssClass { get; set; }
public string Url { get; set; }
public string ImageUrl { get; set; }
public bool HasChildren { get; set; }
public bool Checked { get; set; }
public List<TreeViewItemModel> Items { get; set; }
public IDictionary<string, string> HtmlAttributes { get; set; }
public IDictionary<string, string> ImageHtmlAttributes { get; set; }
public IDictionary<string, string> LinkHtmlAttributes { get; set; }
public bool Selected { get; set; }
}
publicstaticIList<HierarchicalViewModel>GetHierarchicalData(){var result =newList<HierarchicalViewModel>(){newHierarchicalViewModel(){ ID =1, ParentID =null, HasChildren =true, Name ="Parent item"},newHierarchicalViewModel(){ ID =2, ParentID =1, HasChildren =true, Name ="Parent item"},newHierarchicalViewModel(){ ID =3, ParentID =1, HasChildren =false, Name ="Item"},newHierarchicalViewModel(){ ID =4, ParentID =2, HasChildren =false, Name ="Item"},newHierarchicalViewModel(){ ID =5, ParentID =2, HasChildren =false, Name ="Item"}};return result;}publicIActionResultRead_TreeViewData(int? id){var result =GetHierarchicalData().Where(x => id.HasValue ? x.ParentID == id : x.ParentID ==null).Select(item =>new{
id = item.ID,
Name = item.Name,
expanded = item.Expanded,
imageUrl = item.ImageUrl,
hasChildren = item.HasChildren
});returnJson(result);}
HierarchicalViewModel
public class HierarchicalViewModel
{
public int ID { get; set; }
public int? ParentID { get; set; }
public bool HasChildren { get; set; }
public string Name { get; set; }
public bool Expanded { get; set; }
public string ImageUrl { get; set; }
}
By default, the TreeView sends to the remote endpoint the id of the expanded node. To send additional data use the DataSource Data method and provide the name of a JavaScript function which will return a JavaScript object with the additional data.
In order to set up the TreeView component bindings, you need to configure the Read method of its DataSource instance. The URL in this method should refer the name of the method in the PageModel. See the implementation details in the example below, and for the full project with RazorPages examples, visit our GitHub repository.