Thursday, July 19, 2012

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.

 <div id="dTree">  
 </div>  
 <script type="text/javascript">  
   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 + "]"));  
         });  
   }  
 </script>