您的位置:首页 > 其它

一个延迟加载Tree数据的例子

2008-06-27 18:22 501 查看
在有树型结构的应用中,很多时候这棵树的级树和数据量比较大,如果一次加载并生成整颗树时,往往并不可行,于是需要建立一棵延迟加载树,下面就是这样的一个例子,其中的代码我作了命名和注释的修改,原代码是以前摘自别的作者的。

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"

creationComplete="initTreeData()">

<mx:Script>

<![CDATA[

import mx.events.TreeEvent;

import mx.collections.ArrayCollection;

[Bindable]

private var acSiteTreeList:ArrayCollection;

private function initTreeData():void {

var obj:Object;

acSiteTreeList = new ArrayCollection ();

for(var i:int = 0; i <3; i++) {

obj= new Object();

obj["type"] = "folder";

obj["children"] = new ArrayCollection();

//fetch is a property in the dataprovider to check if I have fetched the child nodes previously

//使用fetch来作是否生成下一级节点的依据

obj["fetch"] = false;

obj["label"] = "folder_" + i.toString();

acSiteTreeList.addItem(obj);

}

}

private function setView(event:TreeEvent):void {

if(event.item.type == "folder" && event.item.fetch == false) {

var obj:Object;

var item:Object;

var children:ArrayCollection;

for(var i:int = 0; i <acSiteTreeList.length; i++) {

obj= new Object();

obj["type"] = "node";

//如果当前节点有子节点的话,需要建立一个children的属性,这样会建立无穷级树

//obj["children"] = new ArrayCollection();

obj["fetch"] = false;

obj["label"] = "node_" + i.toString();

item = event.item;

children = item.children;

item.fetch = true;

children.addItem(obj);

acSiteTreeList.itemUpdated(item);

}

}

}

]]>

</mx:Script>

<mx:Canvas width="100%" height="100%">

<mx:Tree id="treeSiteList" dataProvider="{acSiteTreeList}" x="204" y="10" height="582" width="394" itemOpen="setView(event)"></mx:Tree>

</mx:Canvas>

</mx:Application>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: