|
stworthy
|
![](https://jeasyui.com/forum/Themes/default/images/post/xx.gif) |
« Reply #1 on: December 28, 2012, 12:47:38 AM » |
|
Please try the code below. var t = $('#tree').combotree('tree'); // get the tree object var p = t.tree('getParent', node.target); if (p){ p = t.tree('getParent', p.target); } alert(p.text);
|
|
|
Logged
|
|
|
|
varonica
|
![](https://jeasyui.com/forum/Themes/default/images/post/xx.gif) |
« Reply #2 on: December 28, 2012, 01:34:12 AM » |
|
Thanks sir, it works! But Is there a way to get all parents of parent node until a root node ?
|
|
|
Logged
|
|
|
|
patana
|
![](https://jeasyui.com/forum/Themes/default/images/post/xx.gif) |
« Reply #3 on: January 02, 2013, 07:35:23 AM » |
|
Please, take a look at it: <select id="cc" class="easyui-combotree" data-options="url:'../tree/tree_data1.json'" multiple style="width:200px;"></select> <script> var nodes = $('#cc').tree('getChecked', 'indeterminate'); </script>
Hope it works as your desire!
|
|
|
Logged
|
|
|
|
nyr_jack
Newbie
![*](https://jeasyui.com/forum/Themes/default/images/star.gif)
Posts: 31
|
![](https://jeasyui.com/forum/Themes/default/images/post/xx.gif) |
« Reply #4 on: January 05, 2013, 01:11:00 AM » |
|
You can use recursive algorithm, I shared my some codes in General Discussion of forum , I hope it can help you!
|
|
|
Logged
|
|
|
|
patana
|
![](https://jeasyui.com/forum/Themes/default/images/post/xx.gif) |
« Reply #5 on: January 05, 2013, 09:37:21 PM » |
|
Could you please kindly provide a link so it'd be much easier for the owner or passer-by!!!
|
|
|
Logged
|
|
|
|
nyr_jack
Newbie
![*](https://jeasyui.com/forum/Themes/default/images/star.gif)
Posts: 31
|
![](https://jeasyui.com/forum/Themes/default/images/post/xx.gif) |
« Reply #6 on: January 05, 2013, 10:46:51 PM » |
|
|
|
|
Logged
|
|
|
|
|
varonica
|
![](https://jeasyui.com/forum/Themes/default/images/post/xx.gif) |
« Reply #8 on: January 11, 2013, 08:16:30 PM » |
|
Hi nyr_jack, your function is really helpful but unluckily i still don't understand some param that were used in getParentArray(). Could you kindly provide an example ? ![Sad](http://jeasyui.com/forum/Smileys/default/sad.gif) /** * 获得该节点的所有父节点数组(get all parents of current node) * @param basicTree * @param node * @param nodeLevel * @param nodeparentArry * @returns */ function getParentArry(basicTree,node,nodeLevel,parentArry){ if(nodeLevel == 1){ return parentArry; }else{ nodeLevel -= 1; var parent = $(basicTree).tree('getParent',node.target); parentArry.unshift(parent); return getParentArry(basicTree,parent,nodeLevel,parentArry); } }
I just want to get all the parent of node we selected. Please help me ![Cry](http://jeasyui.com/forum/Smileys/default/cry.gif)
|
|
|
Logged
|
|
|
|
varonica
|
![](https://jeasyui.com/forum/Themes/default/images/post/xx.gif) |
« Reply #9 on: January 11, 2013, 08:24:05 PM » |
|
|
|
|
Logged
|
|
|
|
nyr_jack
Newbie
![*](https://jeasyui.com/forum/Themes/default/images/star.gif)
Posts: 31
|
![](https://jeasyui.com/forum/Themes/default/images/post/xx.gif) |
« Reply #10 on: January 12, 2013, 06:13:32 AM » |
|
I wrote a sample, you can only select one node, and when you click button you can get all parents of this seleced node. bellow the main code:
html:
<!-- This button's 'onclick' function need a param, this param specify the tree id --> <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" onclick='getAllParentsOfNode("#tree")'>Get all parents of node</a> <br><br><br> <ul id="tree" class="easyui-tree" data-options="url:'tree_data1.json',animate:true,checkbox:false"></ul>
javascript:
/** * 扩展方法,获得树的节点等级 */ $.extend($.fn.tree.methods,{ getLevel: function(jq, target){ return $(target).find('span.tree-indent,span.tree-hit').length; } });
/** * to get a array of parents of selected node * @param tree - the tree you want to select a node * @param selectedNode - the node that you select * @param nodeLevel - the level of selected node * @param nodeparentArry - use a array to record all parents of selected node * @returns */ function getParentArry(tree,selectedNode,nodeLevel,parentArry){ //end condition: level of selected node equals 1, means it's root if(nodeLevel == 1){ return parentArry; }else{//if selected node isn't root nodeLevel -= 1; //the parent of the node var parent = $(tree).tree('getParent',selectedNode.target); //record the parent of selected to a array parentArry.unshift(parent); //recursive, to judge whether parent of selected node has more parent return getParentArry(tree,parent,nodeLevel,parentArry); } }
function getAllParentsOfNode(tree){ var node = $(tree).tree('getSelected'); alert("The selected node is: " + node.text); var nodeLevel = $(tree).tree('getLevel',node.target); var parentArry = new Array(); var parents = getParentArry(tree,node,nodeLevel,parentArry); if(parents.length > 0){ var parentStr = ""; for(var i = 0; i < parents.length; i++){ parentStr += parents.text + " -> "; } } alert("All parents of selected node:\n" + parentStr); }
|
|
|
Logged
|
|
|
|
varonica
|
![](https://jeasyui.com/forum/Themes/default/images/post/xx.gif) |
« Reply #11 on: July 03, 2013, 02:41:42 AM » |
|
Thanks nyr_jack, Your function works great, but there's a tiny mistake in it. function getAllParentsOfNode(tree){ var node = $(tree).tree('getSelected'); alert("The selected node is: " + node.text); var nodeLevel = $(tree).tree('getLevel',node.target); var parentArry = new Array(); var parents = getParentArry(tree,node,nodeLevel,parentArry); if(parents.length > 0){ var parentStr = ""; for(var i = 0; i < parents.length; i++){ parentStr += parents[i].text + " -> "; // after modify parents[i] it works :) } } alert("All parents of selected node:\n" + parentStr); }
|
|
|
Logged
|
|
|
|
|