views:

569

answers:

4

I have a tree control and after I drop an item in it (which updates the dataprovider) I want it to stay open. I've tried a lot of things including the example at this question which I couldn't get to work so I'm doing something I feel is even more basic. Like this:

[Bindable]
public var open:Object = new Object();

private function dropItemInTree():void{
    open = myTree.openItems;
    //A bunch of code that updates the DP
    reopenTree();
}

public function reopenTree():void{
    for each(var item:XML in open){
        expandParents(item[0]);
    }
}

private function expandParents(node:XML):void {
    myTree.expandItem(node,true,false);
}

But even this is leaving my tree minimized. What's going wrong?

+1  A: 

Sorry, here is the full explanation: The link at the bottom gives the complete explanation along with a full sample.

You must use the Tree control's creationComplete event, not the initialize event, because the data provider is not fully initialized and available until the creationComplete event.

<mx:Tree id="tree1" ... creationComplete="initTree();" >

OR

you could also get the openItems box to indicate the initial open item by setting the expandItem() method to dispatch an itemOpen event. You can do this by specifying the fourth, optional parameter of the expandItem() method to true. The true fourth parameter causes the tree to dispatch an open event when the item opens. The following example shows the use of the fourth parameter:

XMLTree1.expandItem(MailBox.getItemAt(0), true, false, true);

By default, a Tree control is collapsed when it initializes, but you can initialize it so that it is expanded with a specific node selected.

<mx:Script>
    <![CDATA[
        import flash.events.*;
        import mx.events.*;
        import mx.controls.*;
        private function initTree():void {

            XMLTree1.expandItem(MailBox.getItemAt(0), true);
            XMLTree1.selectedIndex = 2;
        }
    ]]>
</mx:Script>

The reference for the tree control is: http://livedocs.adobe.com/flex/3/html/help.html?content=dpcontrols_8.html

Todd Moses
My code should be expanding the tree, just the same as in that example. I'm using the same expandItem method, but for my tree it's not working and I don't know why.
invertedSpear
The problem I have with this example is that my tree is already created, so I don't need to worry about creation complete or anything else. The expandItem method should be working but it's not (it works in the double click function I wrote to open the selected node). Why doesn't it work when I'm looping through the open array?
invertedSpear
even though my problem was a little more abstract than what that example shows I give you an up-vote because anyone having a problem similar to mine needs to understand that example pretty well first.
invertedSpear
A: 

maybe my post at FLEXpert blog could assist you.

FLEXpert
It would if I were trying to open all the folders instead of just the ones that were open prior to the drop action. Thanks anyway.
invertedSpear
A: 

So I finally figured out what was happening. In my drop function I was basically rebuilding the entire DP. While it was almost the same it would have had different UID's inside the flash player, so the objects in the open var no longer had reference the objects in the DP. Luckily there is an ID field in my XML dataprovider so using that I was able to look up the object in the rebuilt DP and finally get the expandItem method to work there.

So my re-open function now looks kind of like this:

public function renderTree():void
    for each(var item:XML in open){
        myTree.expandItem(XML(MyDP..node.(@attr == item.@attr)),true);
        //forcing the type to be XML is VITAL
    }
}
invertedSpear
A: 

Thanks for the answer. I faced a very similar issue. I suspected that it was the player when tree.getItemIndex was not able to find the index of the node that tree.isItemOpen reported was open. Uh huh.

For an XMLListCollection DP the code is similar to:

tree.expandItem(XML(treeData.source.(@id == openedFolder.@id)),true);

ZaleskiDC