flex as实现树的数据递归 系统管理视图会用到的
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
fontFamily="simsun" fontSize="12"
layout="absolute" creationComplete="init()" width="100%" height="100%">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
[Bindable]
private var menus:XML;
public function init():void{
menu.send();
var treeMenus:XML = tree2.dataProvider as XML;
var modules:String = "1,11,12,4";
var ids:Array = modules.split(",");
//Alert.show(treeData.tree.toString()+"","Test");
//不相等就要删除
var trees:XMLList = XMLList(treeData.tree);
tree1.dataProvider = treeData.tree;
for(var i:int=0;i<ids.length;i++){
var str:String = ids[i];
isExists(str,trees);
}
//trace(treeData.tree);
delNoVisibleNode(trees);
trace(trees);
tree2.dataProvider = trees;
}
public function isExists(str:String,trees:XMLList):void{
//trace(str);
for each(var t:XML in trees){
if(t.@id.toString()==str){
t.@visible = true;
}
isExists(str,t.tree);
}
}
//一定要用索引,而且删除的对象要是XMLList里面的元素,不然会报错,或者不能删除
private function delNoVisibleNode(xmllist:XMLList):void{
//for each (var item:XML in xmllist)
for (var i:int=xmllist.length() - 1; i >= 0; i--){
var item:XML=xmllist[i];
if (item.@visible.toString() != "true" && item != null){
delete xmllist[item.childIndex()];
//menuByVisibleFlaseXML.appendChild(item);
//删除符合条件的子节点: delete xml.children()(@type == “car” && @color == “red”);
}else if (item.@visible.toString() == "true"){
//有子节点
if (item.hasComplexContent()){
arguments.callee(item.children());
}
}
}
}
private function LoadMenu(event:ResultEvent):void{
menus = XML(event.result);
var results:XMLList = menus.node;
tree.dataProvider = results;
}
]]>
</mx:Script>
<mx:XML id="treeData" source="assets/data/TreeData.xml" format="e4x"/>
<mx:HTTPService url="assets/data/TreeMenus.xml" id="menu" useProxy="false"
showBusyCursor="true" result="LoadMenu(event)" resultFormat="xml"/>
<mx:HBox width="100%">
<mx:VBox width="46%">
<mx:Tree id="tree" x="10" y="35" width="218" height="397" labelField="@label" />
<mx:Label x="10" y="10" text="Tree Nodes From XML File"/>
</mx:VBox>
<mx:VBox>
<mx:Tree id="tree1" x="10" y="35" width="218" height="397" labelField="@label" />
</mx:VBox>
<mx:VBox width="52%">
<mx:Tree id="tree2" x="10" y="35" width="218" height="397" labelField="@label" />
</mx:VBox>
</mx:HBox>
</mx:Application>