EasyUI Forum
May 20, 2024, 03:48:18 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: getting the root node [Solved]  (Read 9825 times)
devnull
Sr. Member
****
Posts: 431


View Profile
« 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' } } ]
« Last Edit: May 17, 2014, 01:17:07 AM by devnull » Logged

-- Licensed User --
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #1 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]);
}
Logged
devnull
Sr. Member
****
Posts: 431


View Profile
« Reply #2 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

Logged

-- Licensed User --
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #3 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;
}
}
})
Logged
devnull
Sr. Member
****
Posts: 431


View Profile
« Reply #4 on: May 17, 2014, 01:17:58 AM »

Thanks, yes that works.

Can this be added as a standard method ??

Logged

-- Licensed User --
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #5 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.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!