I have determined that a recursive
function that only manipulates the dataProvider is massively faster than this
technique that depends on opening the node and uses getNodeDisplayedAt().
The function below actually builds an
array of matches, but could just as easily break and open a matched
branch. It will traverse and test 36,000 nodes in less than 4
seconds. The selectNode function that uses getNodeDisplayedAt takes
several *minutes* to reach the bottom of the tree.
/*
recursive
precedent tree function
*/
private
function processTreeNode(treeNodeCur:TreeNode,sSearchString:String):Void
{
//Process
current node
var
sLabel:String;
sLabel
= treeNodeCur.getProperty(label);
if
(sLabel.indexOf(sSearchString) -1) { //we
matched strings!
maSearchResults.push(treeNodeCur); //put
a ref to the nod on the array
}
var
aChildren:Array = treeNodeCur.getChildNodes();
for
(var i = 0; i aChildren.length; ++i) {
processTreeNode(aChildren[i],sSearchString);
//Recursive call to children
}
}//processPrecedentTreeNode
This function will open a branch to the
specified node:
/*
Given a
tree node, climbs up the tree opening each parent node
*/
private
function openBranchToNode(treeRef:Tree,treeNodeTarget:TreeNode):Void
{
var
treeDP = treeRef.dataProvider;
var
treeNodeParent:Object = treeNodeTarget.parentNode; //
get parent of specified Node
while
(treeNodeParent != null) {
treeRef.setIsOpen(treeNodeParent,
true); //
open each parent up thru tree
treeNodeParent
= treeNodeParent.parentNode;
}
}//openBranchToNode
From: Robert
Brueckmann [mailto:[EMAIL PROTECTED]
Sent: Thursday, February 17, 2005
12:43 PM
To: flexcoders@yahoogroups.com
Subject: RE: [flexcoders]
Dynamically selecting a Tree node
JoseI posted a similar question a
few weeks back and got a response I was able to work withthe only
drawback to the solution is it expands the entire tree up to the node you want
selected is foundbut its a drawback Im willing to live
with because it does what youre askingIm using this code
in conjunction with a custom HistoryManager method so if a user selects a node
on the tree and then selects a second nodwe and then hits the browsers
back button, I want to go back to the last node they selected in the tree and
the following seems to do the trick when put in a function and your action,
whatever it may be that wants to dynamically select an item in the tree, calls
this function:
function
selectNode(value:String) {
var i:Number=0;
var node = yourTree.getTreeNodeAt(i);
while (node != undefined) {
if (node.getProperty(id) == value) {
yourTree.selectedNode = node;
break;
} else if (yourTree.getIsBranch(node)
!yourTree.getIsOpen(node)){
yourTree.setIsOpen(node, true);
}
i++;
node = yourTree.getNodeDisplayedAt(i);
}
}
The code traverses a tree and compares the
node on the trees attribute of id to the one Im passing to the
function, which was the last id the user had selectedif it matches it
sets that node in the tree to the selected node and then stops the tree
traversalif it doesnt match it checks to see if the node is a
branch and if it is, expands the tree and then continues traversal.
Its my understanding you cant really traverse nodes of the tree
in this fashion if the tree isnt expanded because indexes change, length
of the tree changes, etc, which effect the traversal and access to
nodesso this was the only fix I found, thanks to the programmers on the
list, that was able to traverse the entire tree looking for the one element I
wanted to select in the tree.
Robert L. Brueckmann
Web Developer
Merlin Securities, LLC
595 Madison Avenue
New York, NY
10022
p: 212.822.4821
f: 212.822.4820
From: Jose Lora [mailto:[EMAIL PROTECTED]
Sent: Thursday, February 17, 2005
12:22 PM
To: flexcoders@yahoogroups.com
Subject: [flexcoders] Dynamically
selecting a Tree node
Hi Team,
Its been a while since I post a
question and I was wondering if the Tree experts can give me a hand on this.
Id like to use this value to
traverse a Tree component and select the node that fits the criteria.
Is this even possible?
Any help is appreciated.
Jose Lora
Meredith Corporation
This message contains information
fromMerlin Securities, LLC, or from one of its affiliates, that may be
confidential and privileged. If you are not an intended recipient, please
refrain from any disclosure, copying, distribution or use of this information
and note that such actions are prohibited. If you have received this
transmission in error, please notify the sender immediately by telephone or by
replying to this transmission.
Merlin Securities, LLC is a registered
broker-dealer. Services offered