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

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

1.<?xml version="1.0" encoding="utf-8"?>
2.<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
3.    creationComplete="initTreeData()">
4.    <mx:Script>
5.        <![CDATA[
6.           
7.            import mx.events.TreeEvent;
8.            import mx.collections.ArrayCollection;
9.           
10.           
11.            [Bindable]
12.            private var acSiteTreeList:ArrayCollection;
13.           
14.            private function initTreeData():void {
15.                var obj:Object;
16.                acSiteTreeList = new ArrayCollection ();
17.                for(var i:int = 0; i <3; i++) {
18.                    obj= new Object();
19.                    obj["type"] = "folder";
20.                    obj["children"] = new ArrayCollection();
21.                    //fetch is a property in the dataprovider to check if I have fetched the child nodes previously
22.                    //使用fetch来作是否生成下一级节点的依据
23.                    obj["fetch"] = false;
24.                    obj["label"] = "folder_" + i.toString();
25.                    acSiteTreeList.addItem(obj);
26.                }
27.            }
28.           
29.            private function setView(event:TreeEvent):void {
30.                if(event.item.type == "folder" && event.item.fetch == false) {
31.                   
32.                   
33.                    var obj:Object;
34.                    var item:Object;
35.                    var children:ArrayCollection;
36.                    for(var i:int = 0; i <acSiteTreeList.length; i++) {
37.                        obj= new Object();
38.                        obj["type"] = "node";
39.                        //如果当前节点有子节点的话,需要建立一个children的属性,这样会建立无穷级树
40.                        //obj["children"] = new ArrayCollection();                     
41.                        obj["fetch"] = false;
42.                        obj["label"] = "node_" + i.toString();
43.                        item = event.item;
44.                        children = item.children;
45.                        item.fetch = true;
46.                        children.addItem(obj);
47.                        acSiteTreeList.itemUpdated(item);
48.                    }
49.                }
50.            }
51.        ]]>
52.    </mx:Script>
53.    <mx:Canvas width="100%" height="100%">
54.        <mx:Tree id="treeSiteList" dataProvider="{acSiteTreeList}" x="204" y="10" height="582" width="394" itemOpen="setView(event)"></mx:Tree> 
55.    </mx:Canvas>
56.</mx:Application>

你可能感兴趣的:(actionscript)