Flex3 JSON 数据填充树形控件

春天到了,我也来种种树!

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()" fontSize="14">
	<mx:Script>
		<![CDATA[
			import mx.collections.ArrayCollection;
			import mx.controls.Alert;
			import com.test.TreeVO;
			
			public var open:Object = new Object();
		
			public var refreshData:Boolean = true;
			
			//原始测试数据
			private var datas:Array = [
						{name:'相册',dir_id:1,parent_id:0,del:0},
						{name:'我的照片',dir_id:2,parent_id:1,del:0},
						{name:'朋友的照片',dir_id:3,parent_id:1,del:0},
						{name:'我的文件',dir_id:4,parent_id:0,del:0},
						{name:'工具软件',dir_id:5,parent_id:4,del:0},
						{name:'系统软件',dir_id:6,parent_id:4,del:0},
						{name:'影音娱乐',dir_id:7,parent_id:0,del:0}
						];
			
			[Bindable]
			private var treeSource:ArrayCollection = new ArrayCollection(); //tree Data
			

			
			private function chg():void{
				Alert.show(menu.selectedItem.parent_id.toString(),"系统消息");
			}
			
			private function init():void{
				if(0 != datas.length){
					for each (var node:TreeVO in datas){
						if(0 == node.parent_id){
							var treeNode:Object = new Object();
							trace("node:" + node.name);
							treeNode.name = node.name;
							treeNode.dir_id = node.dir_id;
							treeNode.parent_id = node.parent_id;
							var sub_arr:ArrayCollection = new ArrayCollection();
							for each (var subnode:TreeVO in datas){
								if(subnode.parent_id == node.dir_id){
									trace("subNode:" + subnode.name);
									sub_arr.addItem(subnode);
								}
							}
							if(0 != sub_arr.length){
								treeNode.children = new Array();
								treeNode.children = sub_arr;
							}
							treeSource.addItem(treeNode);
						}
					}
				}
			}
			
            private function render():void{
                if(refreshData){

                    // Refresh all rows on next update.
                    menu.invalidateList();
                    refreshData = false;
                    menu.openItems = open;
                    // Validate and update the properties and layout
                    // of this object and redraw it, if necessary.
                    menu.validateNow();
                }

            }
		]]>
	</mx:Script>
	<mx:Tree render="render()" showRoot="false" change="chg()" id="menu" dataProvider="{treeSource}" labelField="name" width="300" height="100%">
	</mx:Tree>
</mx:Application>

 

 

package com.test
{
	public class TreeVO
	{
		public function TreeVO()
		{
		}
		private var _name:String;
		private var _dir_id:int;
		private var _parent_id:int;
		
		public function set name(name:String):void{
			this._name = name;
		}
		
		public function get name():String{
			return this._name;
		}
		
		public function set dir_id(dir_id:int):void{
			this._dir_id = dir_id;
		}
		
		public function get dir_id():int{
			return this._dir_id;
		}
		
		public function set parent_id(parent_id:int):void{
			this._parent_id = parent_id;
		}
		
		public function get parent_id():int{
			return this._parent_id;
		}

	}
}

 

你可能感兴趣的:(json,xml,软件测试,Adobe)