Only thing I see is that in doPopulate() I’d use tree.getTreeNodeAt(0) rather than firstVisibleNode.  Have you put in trace statements to make sure that everything is executing as expected?

 


From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of Tim Blair
Sent: Monday, May 09, 2005 4:51 AM
To: flexcoders@yahoogroups.com
Subject: [flexcoders] Pre-selecting Tree nodes

 

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="" label="Assets">
    <item data="" label="Brand">
      <item data="" label="Breakfast">
        <item data="" label="Coffee Makers" />
        <item data="" label="Collections" />
        <item data="" label="Kettles" />
        <item data="" label="Toasters" />
      </item>
      <item data="" label="Cooking Appliances">
        <item data="" label="Fryers" />
        <item data="" label="Steamers" />
        <item data="" label="Tabletop" />
      </item>
      <item data="" label="Food Preparation">
        <item data="" label="Juicers" />
        <item data="" label="Kitchen Scales" />
        <item data="" 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

Reply via email to