在Flex中灵活运用openItems实现树刷新后打开原始节点

法一:(直接使用openItems)

<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" initialize="initTree()" viewSourceURL="srcview/index.html"> <mx:Script> <!--[CDATA[ [Bindable] public var open:Object=new Object(); [Bindable] public var refreshData:Boolean=false; [Bindable] public var switchObj:Object=new Object(); [Bindable] public var firstObj:Object=new Object(); [Bindable] public var firstObj1:Object=new Object(); [Bindable] public var firstObj2:Object=new Object(); [Bindable] public var firstObj3:Object=new Object(); [Bindable] public var provider:String="firstObj"; private function initTree():void { firstObj=new Object(); firstObj.label="Foods"; firstObj.children=new Array(); firstObj1.label="Fruits"; firstObj1.children=new Array(); firstObj2.label="Oranges"; firstObj1.children[0]=firstObj2; firstObj.children[0]=firstObj1; } public function changeProvider():void { open=SampleTree.openItems; refreshData=true; if (provider == "firstObj") { firstObj3.label="XXXX"; firstObj.children[1]=firstObj3; switchObj=firstObj; provider="switchObj"; SampleTree.dataProvider=switchObj; } else { firstObj3.label="YYYY"; firstObj.children[1]=firstObj3; provider="firstObj"; SampleTree.dataProvider=firstObj; } renderTree(); } public function renderTree():void { if (refreshData) { SampleTree.invalidateList(); refreshData=false; SampleTree.openItems=open; SampleTree.validateNow(); } } ]]--> </mx:Script> <mx:Tree id="SampleTree" width="250" dataProvider="{firstObj}" labelField="label"/> <mx:Button label="Change Data Provider" click="changeProvider()"/> </mx:Application>

 

法二:(间接使用openItems)

 思路大概如下: 1、首先拿到树中被打开的节点; 2、根据打开的节点进行判断,当打开节点为空时则打开根目录;如果打开节点不为空时,则将打开节点存入一个数组中,然后再调用openItems实现节点的打开。大致代码如下: var openOrg:Array = typeTree.openItems as Array; typeTree.invalidateList(); typeTree.validateNow(); // 保持打开状态 if(openOrg.length == 0) { typeTree.selectedIndex = 0; var root:XML = typeTree.selectedItem as XML; typeTree.expandItem(root, true); }else { var open:Array = new Array(); for each(var node:XML in openOrg) { if(node) { node = ParseXmlUtil.getFolderXMLById(node.@id, typeXML);//从树中获得节点 open.push(node); } } typeTree.openItems = open;

你可能感兴趣的:(function,object,Flex,application,button,encoding)