PDA

View Full Version : Finding child anywhere in tree



Allan
22 Feb 2008, 4:14 AM
Hi,

a small fragment of JavaScript code that might help those of you trying to find a child buried deeply in a tree.

Mildly tested :)



/**
* Finds the first child that has the attribute with the specified value.
* Looks recursively down the tree to find the child.
* @param {String} attribute The attribute name
* @param {Mixed} value The value to search for
* @return {Node} The found child or null if none was found
*/
function findChildRecursively(tree,attribute, value) {
var cs = tree.childNodes;
for(var i = 0, len = cs.length; i < len; i++) {
if(cs[i].attributes[attribute] == value){
return cs[i];
}
else {
// Find it in this tree
if(found = findChildRecursively(cs[i], attribute, value)) {
return found;
}
}
}
return null;
}


There may be a way to use cascade() to achieve the same effect but I can't see how immediately!

Allan

dantheman
22 Feb 2008, 9:41 AM
What advantage does this have over Ext's TreeNode method?


findChild( String attribute, Mixed value )

--dan

Allan
22 Feb 2008, 1:24 PM
The findChild method only looks at the children of the given node, it doesn't search through the children's children and further down the tree.

Allan

dantheman
22 Feb 2008, 1:55 PM
The findChild method only looks at the children of the given node, it doesn't search through the children's children and further down the tree.I have never used the Ext method, but examining the source now, you are right.

Honestly, it wouldn't have occured to me that a so-named function wouldn't work as yours does ... :-|
So, to get technical, yours should be called findDescendent.

In any case, the Ext way of doing this is with cascade and bubble (although you have the
unfortunate semantic of returning false when finding what you're looking for :D ).

--dan

jsakalos
22 Feb 2008, 2:34 PM
What about Node::cascade(...) method.

Animal
25 Feb 2008, 3:27 AM
Node::cascade doesn't return anything. Really findChild should originally have been fully recursive. This function should be added. It could be very useful.

efattal
13 Mar 2008, 1:17 PM
And what would happen in case of AsyncTreeNode.... ?

Allan
13 Mar 2008, 2:37 PM
It works with AsyncTreeNodes if they have been loaded! Obviously unloaded tree nodes will exist only on the server and ought to be searched for there ...

paralmix
6 Apr 2008, 11:37 AM
Your function doesn't work, becouse if node isn't populate
tree.childNodes collection is empty

sfwalter
6 Apr 2008, 12:49 PM
yeah you basically need to do an expandAll() on the root before being able to search the whole tree.

paralmix
9 Apr 2008, 12:19 AM
yes, i knew, but I think that not a solve this problem

zalagardin
15 Apr 2008, 11:53 PM
Can u post a cascade function example? I don't understand how it works and i need a similar functionality to search values not empty in a tree.
Thanks

Erikk75
17 Apr 2008, 8:33 AM
I have a asynctreenode, and even if I load the whole tree (all children are returned in the json data for every single node in the tree), i cannot search for a specific node by providing its id, without expanding the whole tree.
Is it possible to search for a specific node without exanding the whole tree, so that I could for example expand only a specific node when I reload the tree ?

brumby007
23 Apr 2008, 12:34 AM
I have a asynctreenode, and even if I load the whole tree (all children are returned in the json data for every single node in the tree), i cannot search for a specific node by providing its id, without expanding the whole tree.
Is it possible to search for a specific node without exanding the whole tree, so that I could for example expand only a specific node when I reload the tree ?

i have the same problem.
no one knows?

NoahK17
16 May 2008, 2:57 PM
@brumby007: No, there is no solution without loading all the nodes of the tree using expandAll();

My question about searching within a tree is this...

I need to search for multiple children deep within a tree, and if found show that child (plus all the parent/grandparent nodes). I'm having a difficult time w/ my search feature for a tree that is just 3 levels deep.

Any thoughts?

NoahK17
16 May 2008, 3:17 PM
I found a partial solution here: http://extjs.com/forum/showthread.php?p=60265

Still working on the "show me all the children of a matching node" thing I need :)

ZooKeeper
18 Jul 2008, 4:12 AM
Is there any Ext implementation of recursive node search as in the first post?

bernardsch
27 Mar 2010, 1:52 PM
You can add this code :


Ext.override(Ext.tree.TreeNode, {
findDescendant: function(attribute, value) {
var children = this.childNodes;
for(var i = 0, l = children.length; i < l; i++) {
if(children[i].attributes[attribute] == value){
return children[i];
} else if(node = children[i].findDescendant(attribute, value)) {
return node;
}
}
return null;
}
});

jsakalos
28 Mar 2010, 1:26 AM
I have found this method in Ext.data.Tree of 3.1.x svn branch:


/**
* Finds the first child by a custom function. The child matches if the function passed returns <code>true</code>.
* @param {Function} fn A function which must return <code>true</code> if the passed Node is the required Node.
* @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the Node being tested.
* @return {Node} The found child or null if none was found
*/
findChildBy : function(fn, scope){
var cs = this.childNodes;
for(var i = 0, len = cs.length; i < len; i++) {
if(fn.call(scope||cs[i], cs[i]) === true){
return cs[i];
}
}
return null;
},

alliance02
29 Sep 2010, 1:13 AM
Hi All,

I want to hide the children of a particular node.
Is this possible?
Can anyone help?

Thanks

jsakalos
29 Sep 2010, 1:29 AM
Hmm, I thought that "hiding children of a node" equals collapsing that node. Tree node doesn't have hide() method.

alliance02
29 Sep 2010, 1:43 AM
Thanks for the prompt reply..
I can hide the children statically here is the code:

items:[
{
xtype: 'treepanel',
id: 'tree',
title: 'My Tree',
width: 400,
height: 250,
root: {
text: 'first'
,expanded:true
,id: 'first'
, nodeType:'async'
,children:[
{text:'test',leaf:true, id: '1', hidden: true}
,{text:'test',leaf:true, id: '2'}
]
}
}
]

but i want to hide specific children during render of the tree panel..

If i will collapse the node, all of the children will not be visible to the user.
Also, the user can simply uncollapsed that node..

Thanks in advance for your help.