2 Replies Latest reply on Jun 6, 2008 7:38 PM by rodrix79

    How do I select an inner tree node?

    rodrix79
      Hi. I am trying to select an inner node within a tree without much luck so far. I am doing something like this:

      public function selectNodes():void{
      for(var i:int = 0; i < nodesToSelect.length; i++){
      var name:String = nodesToSelect.getItemAt(i).name;
      var child:DisplayObject = tree.getChildByName(name);
      tree.selectedItems.push(child);
      }
      }

      Still, nothing happens. The tree.getChildByName(name) command returns null. The variable name does have a valid name when I inspect it (although it is a name from an inner node, not a root node). What am I doing wrong? Thanx in advance...
        • 1. Re: How do I select an inner tree node?
          ntsiii Level 3
          When working with data-driven components, you will always be working with the underlying data, not with rendered instances of the ui components. So try:
          (Assuming nodesToSelect is an XMLListCollection:)
          tree.selectedItems.push(nodesToSelect.getItemAt(i));

          Tracy
          • 2. Re: How do I select an inner tree node?
            rodrix79 Level 1
            Ok. I think I solved the problem. Just for the record, I extended the Tree component in order to have a method which receives both an attribute name and value. This method selects any object which has an attribute with that value. Here is the code:

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Tree xmlns:mx=" http://www.adobe.com/2006/mxml">
            <mx:Script>
            <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.collections.IViewCursor;
            import mx.collections.ICollectionView;

            public function selectNodeByAtt(att:String, attValue:Object):Object {
            var ret:Object = null;
            if(this.dataDescriptor.getChildren(this.firstVisibleItem).length > 0){
            ret = selectNode(this.dataDescriptor.getChildren(this.firstVisibleItem), att, attValue)
            }
            if(ret != null) {
            this.validateNow();
            if(this.allowMultipleSelection)
            this.selectedItems = this.selectedItems.concat(ret);
            else
            this.selectedItem = ret;
            }

            return ret;
            }

            private function selectNode(children:ICollectionView, att:String, attValue:Object):Object {
            var cursor:IViewCursor = children.createCursor();
            var obj:Object = null;

            while (!cursor.afterLast) {
            obj = cursor.current;
            if(obj[att] == attValue){
            break;
            } else {
            if(this.dataDescriptor.hasChildren(obj))
            obj = selectNode(this.dataDescriptor.getChildren(obj), att, attValue);
            }
            cursor.moveNext();
            }

            return obj;
            }
            ]]>
            </mx:Script>
            </mx:Tree>

            Thanks anyway...