EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: Shalini on July 18, 2013, 09:09:18 PM



Title: How to get child nodes (only folders and not leaf nodes) from easy ui tree
Post by: Shalini on July 18, 2013, 09:09:18 PM
Hi,
I need to get the child nodes from easy ui tree. And that child nodes should only include the sub  folders and not the leaf nodes. I had achieved this with the below code. But this might have performance issue due to the for loop if i have a huge tree. Hence i would like to know if there is any other way where we can get only the sub folders. Please help me resolve this.
Code:
var node = $('#tt').tree('getSelected');
var child_node = $('#tt').tree('getChildren', node.target); // This gives all the childs i.e folders and leaf nodes.
var count = 0;
for(var i=0; i< child_node.length; i++)
{
   var child = $('#tt').tree('isLeaf', child_node.target); //Boolean - true if the child_node is leaf, else false
   if(!child)
      count = count + 1; // Increment count and get the count of Folders when leaf is false
}
alert('count' + count);

Regards,
Shalini S.


Title: Re: How to get child nodes (only folders and not leaf nodes) from easy ui tree
Post by: stworthy on July 20, 2013, 06:53:53 AM
A better solution is to extend a new method. The code below is the simple implementation of the 'getFolders' method.
Code:
$.extend($.fn.tree.methods,{
getFolders: function(jq, target){
var nodes = [];
var ul = $(target).length ? $(target).next() : jq;
ul.find("div.tree-node:has(span.tree-folder)").each(function(){
nodes.push(jq.tree('getNode',this));
});
return nodes;
}
});

To get the selected node's child folders, try this:
Code:
var node = $('#tt').tree('getSelected');
var folders = $('#tt').tree('getFolders', node.target);
console.log(folders);


Title: Re: How to get child nodes (only folders and not leaf nodes) from easy ui tree
Post by: Shalini on August 06, 2013, 02:50:38 AM
Hi stworthy,

Thank you so much for your solution..  :)
It works great.

Regards,
Shalini S.