Kendo UI: Iterations on Hierarchical data Source

If you are looking into populating the hierarchical data source using Keno framework, First of all you need to use [ kendo.data.HierarchicalDataSource] and the structure should be something like

 var dataSource = kendo.data.HierarchicalDataSource({  
type: "json",
transport: {
read: {
url: "/test",
datatype: json,
type: "POST",
contentType "application/json;charset=utf-8",
}
},
schema: {
model: {
hasChildren: "hasChildren"
}
}
});

Here hasChildren value comes from your controller with the data,depending upon if the parent has any child node attach. so when you return data from controller make sure you have hasChildren field defined and that should be a bool value.

 



var Tree;
var Root;
$(document).ready(function () {
Root = new kendo.data.HierarchicalDataSource({
transport: {
read: {
url: document.URL + "/GetTreeData/",
dataType: "json"
}
},
schema: {
model: {
id: "Id",
hasChildren: "HasChildren"
}
}
});
Tree = $("#dTree").kendoTreeView({
dataSource: Root,
dataValueField: "Id",
dataTextField: "Name",
select: function (e) {
var selecteddata = null;
if ($.browser.msie)
selecteddata = Root.getByUid(e.node.attributes[1].nodeValue);
else
selecteddata = Root.getByUid(e.node.attributes[0].nodeValue);
UnitEventArgunments.Selecteddata = selecteddata;
UnitEventArgunments.SelectedTreeNode = e;
$(document).trigger(OnNodeEvents.OnNodeUnitClicked, UnitEventArgunments);
}
});
//This is the event that will be triggered when node has been added
$(document).bind(OnNodeEvents.OnNodetAdded, function (e, args) {
refreshTreeView(args);
});
});
function refreshTreeView(e) {

var uId = new Array();
if ($.browser.msie)
selectedNodeData = Root.getByUid(e.node.attributes[1].nodeValue);
else
selectedNodeData = Root.getByUid(e.node.attributes[0].nodeValue);
var childData = selectedNodeData.children._data;
for (var i = 0; i < childData.length; i++) {
uId[i] = childData[i].uid;
}
for (var j = 0; j < uId.length; j++) {
var item = Tree.data("kendoTreeView").dataSource.getByUid(uId[j]);
Tree.data("kendoTreeView").dataSource.remove(item);
}
appendNode(selectedNodeData);
}
function appendNode(selectedNodeData) {
$.post("/GetTreeData", { Id: selectedNodeData.Id },
function (data) {
Tree.data("kendoTreeView").append(data, $("[data-uid=" + selectedNodeData.uid + "]"));
});
}

Published by Nirbhay

Technology Geek,Blogger,Reviewer

3 thoughts on “Kendo UI: Iterations on Hierarchical data Source

  1. hello you
    i interested about a results from controller return.
    I want see it outside my javascript but i don't know how do.
    pl help me

    Email:ngovu.dl@gmail.com
    yahoo:butter.ngo610

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: