开发者

Flex Tree not re-expanding

开发者 https://www.devze.com 2022-12-19 11:01 出处:网络
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 t

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?


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
    }
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消