EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: devnull on May 16, 2014, 08:30:24 PM



Title: getting the root node [Solved]
Post by: devnull on May 16, 2014, 08:30:24 PM
I am trying to get the top parent from a child node, however the getRoot() always returns the very first root and not the root of the child:

if I call getRoot() from a 'usercopy' node, then the root returned is the 'user' node and not the 'usercopy' node.

How can I get the correct root node ?

Code:

onClick:function(node){
    var top = $(this).tree('getRoot', node.target)
}

[ { id: 'user',
    text: 'User',
    iconCls: 'icon-user',
    state: 'open',
    children: [ [Object], [Object] ],
    attributes: { vpath: 'user' } },
  { id: 'usercopy',
    text: 'User',
    iconCls: 'icon-user',
    state: 'open',
    children: [ [Object], [Object] ],
    attributes: { vpath: 'usercopy' } } ]


Title: Re: getting the root node
Post by: stworthy on May 16, 2014, 10:21:56 PM
The 'getRoot' method returns the first top root. To get all the root nodes, please try 'getRoots' method.
Code:
var roots = $('#tt').tree('getRoots');
for(var i=0; i<roots.length; i++){
  console.log(roots[i]);
}


Title: Re: getting the root node
Post by: devnull on May 16, 2014, 11:32:37 PM
Thanks, but I don't want to get all of the roots, I just want to get the top parent of the selected node.

In the previous json code there are 2 nodes:

1) user
2) usercopy
- Child 1
-- Child 1 A
--- Child 1 A A
- Child 2

I want to be able to get the "usercopy" node from any of the Children below it.

For example, when [Child 1 A A]  or [Child 1] or [Child 2] is clicked, I want to get the [usercopy] node.

Thanks



Title: Re: getting the root node
Post by: stworthy on May 17, 2014, 12:40:49 AM
Please try to override the 'getRoot' method as below:
Code:
$.extend($.fn.tree.methods, {
getRoot: function(jq, nodeEl){
if (nodeEl){
var target = nodeEl;
var p = jq.tree('getParent', target);
while(p){
target = p.target;
p = jq.tree('getParent', p.target);
}
return jq.tree('getNode', target);
} else {
var roots = jq.tree('getRoots');
return roots.length ? roots[0] : null;
}
}
})


Title: Re: getting the root node [Solved]
Post by: devnull on May 17, 2014, 01:17:58 AM
Thanks, yes that works.

Can this be added as a standard method ??



Title: Re: getting the root node [Solved]
Post by: stworthy on May 17, 2014, 02:01:39 AM
Yes, this enhanced function will be integrated into next version. You can download the patch file(for version 1.3.6) from http://www.jeasyui.com/download/downloads/jquery-easyui-1.3.6-patch.zip, which has integrated this function.