Hello,
Does anyone have an example of pre-selecting nodes in a Tree component
based on the data item associated with that node? I'm tearing my hair
out here...
Example: I have the following files (tree.xml and tree.mxml) -- on
initialisation (or button click etc) I would like the nodes with the
data value of 39, 40 and 47 ("Collections", "Kettles" and "Juicers") to
be selected, and the tree open at the appropriate places. I've pasted
the test code I have in place.
What happens with the test code below is that on the first click of the
"populate" button (without manually expanding any of the tree nodes
first), nothing happens. If I then expand both the root ("Assets") node
and the "Brand" node below it, click the button again will open up the
correct nodes but will mark the root node as selected. After these
nodes have been displayed, I can close all nodes and then the populate()
works as expected in opening the correct nodes, but again selects on the
root node.
Any help greatly appreciated!
Cheers,
Tim.
tree.xml:
=========
<?xml version="1.0" encoding="utf-8"?>
<tree lang="en">
<item data="1" label="Assets">
<item data="13" label="Brand">
<item data="26" label="Breakfast">
<item data="38" label="Coffee Makers" />
<item data="40" label="Collections" />
<item data="39" label="Kettles" />
<item data="37" label="Toasters" />
</item>
<item data="27" label="Cooking Appliances">
<item data="46" label="Fryers" />
<item data="41" label="Steamers" />
<item data="45" label="Tabletop" />
</item>
<item data="27" label="Food Preparation">
<item data="47" label="Juicers" />
<item data="49" label="Kitchen Scales" />
<item data="48" label="Various Items" />
</item>
</item>
</item>
</tree>
tree.mxml:
==========
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml">
<!-- define the model we want to use -->
<mx:Model id="treeModel" source="tree.xml" />
<mx:Script>
<![CDATA[
// the node data IDs we want to select automatically
var nodes:String = "39,40,47";
// find a given string in a delimited list of strings
function listFind(list:String, item:String,
delim:String): Number {
// by default the delimeter is a comma
if (delim == undefined) { delim = ","; }
// split the list to an array to loop through
var aList:Array = list.split(delim);
// loop through each array item
for (var i = 0; i < aList.length; i++) {
// if this item matches the one we want,
return the position
if (aList[i] == item) { return (i + 1);
}
}
// by default return a fail (0)
return 0;
}
// populate the tree based on the node data items above
function populate(node:Object): Void {
// initially, close this node
tree.setIsOpen(node, false);
// check if this node needs selecting
if (listFind(nodes, node.getData())) {
// open the parent node and select this
node
openNode(node.parentNode, true);
tree.selectedNodes.push(node);
}
// loop through all children and call this
function recursively
for (var i = 0; i < node._childNodes.length;
i++) {
populate(node._childNodes[i]);
}
}
// function to recursively open a tree node
function openNode(node:Object): Void {
// recursively go up through the parents and
open each node
if (node.parentNode != undefined) {
openNode(node.parentNode); }
// and finally open this node
tree.setIsOpen(node, true);
}
// pre-select the required tree nodes
function doPopulate(): Void {
// un-select everything
tree.selectedNodes = [];
// and now populate from the top down
populate(tree.firstVisibleNode);
}
]]>
</mx:Script>
<!-- display the tree -->
<mx:Tree id="tree" dataProvider="{treeModel}"
multipleSelection="true"
defaultLeafIcon="TreeFolderOpen" width="300"
height="400" />
<!-- populate button -->
<mx:Button label="Populate" click="doPopulate()" />
</mx:Application>
--
-------------------------------------------------------
Badpen Tech - CF and web-tech: http://tech.badpen.com/
-------------------------------------------------------
RAWNET LTD - independent digital media agency
"We are big, we are funny and we are clever!"
New site launched at http://www.rawnet.com/
-------------------------------------------------------
This message may contain information which is legally
privileged and/or confidential. If you are not the
intended recipient, you are hereby notified that any
unauthorised disclosure, copying, distribution or use
of this information is strictly prohibited. Such
notification notwithstanding, any comments, opinions,
information or conclusions expressed in this message
are those of the originator, not of rawnet limited,
unless otherwise explicitly and independently indicated
by an authorised representative of rawnet limited.
-------------------------------------------------------
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/flexcoders/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/