flex 递归树

import mx.collections.ArrayCollection;
			private var treeData:ArrayCollection=new ArrayCollection([
				{id: 0,pid:-1,text:'0(0级)'},
				{id: 1,pid:0,text:'1(一级)'},
				{id: 2,pid:4,text:'2.1.1(三级)'},
				{id: 3,pid:0,text:'2(一级)'},
				{id: 4,pid:3,text:'2.1(二级)'},
				{id: 5,pid:0,text:'3(一级)'},
				{id: 6,pid:5,text:'3.1(二级)'},
				{id: 7,pid:0,text:'4(一级)'},
				{id: 8,pid:7,text:'4.1(二级)'},
				{id: 9,pid:4,text:'2.1.2(三级)'},
				{id:11,pid:2,text:'2.1.1.1(四级)'},
				{id:12,pid:2,text:'2.1.1.2(四级)'},
				{id:13,pid:5,text:'3.2(二级)'},
				{id:19,pid:5,text:'3.3(二级)'}
			]);
			
			
			var treexml:String = "";
			
			public function init():void{
				var o:Object = treeData.getItemAt(0);
				showTree(o);
				trace(treexml);	
			}
			
			
			//判断字母还是数字
			public function showTree(tree:Object):void
			{
			
				var id1:String = tree["id"];
				var pid1:String = tree["pid"];
				var text1:String = tree["text"];
				treexml += "<tree id='"+id1+"' pid='"+pid1+"' text='"+text1+"'>"
				for(var j:int=1;j<treeData.length;j++)
				{
					var obj:Object = treeData.getItemAt(j);
					var id:String = obj["id"];
					var pid:String = obj["pid"];
					var text:String = obj["text"];
					if(pid == id1)
					{
						showTree(treeData.getItemAt(j));
					}
					if(j==treeData.length){
						treexml +="</tree>"
					}
					
				}
				treexml +="</tree>"
				
			}

 

你可能感兴趣的:(Flex)