Flex树形菜单动态加载 Flex Tree Dynamic Loading

Categories: Flex; Tagged with: ; @ September 27th, 2008 17:43

[小站博客均为原创, 转载请保留以下信息:

作者:http://liguoliang.com 欢迎访问:Adobe上海用户组: http://riashanghai.com ]

1. 设计目标:png-1163

Flex中Tree动态加载,即在点击之后才加载下级目录的内容.

2. 概要设计:

Tree的属性:
dataDescriptor=”{new CategoryTreeDataDiscriptor()}”
dataProvider=”{Category.root}” id=”treeCategory”
通过root对象与dataDescriptor提供给Tree数据.

初始化成功后即在Tree上监听函数:
treeCategory.addEventListener(TreeEvent.ITEM_OPEN, treeOpenHandler);
在TreeEvent.ITEM_OPEN发生之后,读取数据库或其他数据源的数据,以建立其下级目录.

3. 实现代码:

在用户登录成功之后会立即读取数据并新建其对应的根目录,并将其赋值给静态变量Category.root.
为了提示用户目录正在加载,同时也作为已经加载过的标记,为每个新建的Category中加入一个fake Category;
实现方法: category的代码,在每次新建category的时候都会增加fake,当建立fake时不会增加.

	public class Category
	{
		public static var fake:Category = new Category(true);
		fake.label = "Loading..";

		[Bindable]
		public static var root : Category;

		public var id:int;
		public var label : String;
		public var subCats : ArrayCollection;
		public var notes : ArrayCollection;
		public var parent : Category;

		public function Category(isFake:Boolean=false) {
			if(!isFake) {
				addSubCat(fake);
			}
		}....

treeOpenHandler:

如果为第一次展开[即其子目录只有一个,并且为fake],则读取数据源数据,建立其下级目录对象,将下级目录对象加入到该目录的子目录ArrayCollection中.

//Tree响应函数,展开后读取子目录
private function treeOpenHandler(e:TreeEvent):void {
openingItem = e.item as Category;
LogUtils.defaultLog.info(openingItem.label + " is opened");
if(openingItem == Category.fake) {
Alert.show("正在加载,请稍候...");
return;
}
if((openingItem as Category).subCats == null) {
return;
}
if((openingItem as Category).subCats.length !=1 || (openingItem as Category).subCats[0] != Category.fake) {
return;
}
var sql:String =
"SELECT * FROM Cat WHERE user_ID=" + currentUser.id + " AND " + "parent_ID=" + openingItem.id + ";";
SQLUtils.createAndExecuteStatement(conn,sql,null,loadSubCatsOk,loadSubCatsError);
}

//当load正常时,运行,增加子目录,首先会删除fake.
private function loadSubCatsOk(e:SQLEvent):void {
var CatsFromDB:SQLResult = e.target.getResult();
if(CatsFromDB.data == null) {
openingItem.subCats.removeAll();
LogUtils.defaultLog.info(openingItem.label + “没有子目录”);
return;
}
openingItem.subCats.removeAll();
for(var i:int=0; i
var subCat:Category = new Category();
subCat.id = CatsFromDB.data[i].cat_ID;
subCat.label = CatsFromDB.data[i].label;
subCat.parent = openingItem;
LogUtils.defaultLog.info(“子目录名称:” + CatsFromDB.data[i].label);
openingItem.addSubCat(subCat);
}
}

<->



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.