背景:一次載入所有的資料,效能非常低下,所以預設只載入第一層,當點擊的時候再去載入下面的孩子資料
點擊後 ---------------------------->
<?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="initTree()"><mx:Script><![CDATA[import mx.events.TreeEvent;import mx.rpc.events.ResultEvent;//當前開啟的節點項private var currentOpenItem:XML; //第一層樹:父級樹private var parentTreeStr:String = "<>"+"<country countryId='0001' countryName='中國' isLoad='false' isBranch='true' parentId='0'></country>"+"<country countryId='0002' countryName='美國' isLoad='false' isBranch='true' parentId='0'></country>"+"<country countryId='0003' countryName='英國' isLoad='false' isBranch='true' parentId='0'></country>"+"<country countryId='0004' countryName='法國' isLoad='false' isBranch='true' parentId='0'></country>"+"<country countryId='0005' countryName='德國' isLoad='false' isBranch='true' parentId='0'></country>"+"</>"; //第二層樹:子類層private var childTreeStr:String = "<>"+"<country countryId='00010001' countryName='北京市' isLoad='false' isBranch='true' parentId='0001'></country>"+"<country countryId='00010002' countryName='上海市' isLoad='false' isBranch='true' parentId='0001'></country>"+"<country countryId='00010003' countryName='廣州市' isLoad='false' isBranch='true' parentId='0001'></country>"+"<country countryId='00010004' countryName='深圳市' isLoad='false' isBranch='true' parentId='0001'></country>"+"<country countryId='00010005' countryName='武漢市' isLoad='false' isBranch='true' parentId='0001'></country>"+"</>";//初始化樹:預設只載入第一層private function initTree():void{var treeXML:XMLList = new XMLList(this.parentTreeStr);this.tree.dataProvider = treeXML;}//開啟樹節點 非同步延時載入private function itemOpenHandler(event:TreeEvent):void{if(event.type == TreeEvent.ITEM_OPEN){var e:TreeEvent = TreeEvent(event);currentOpenItem = XML(e.item);/**此時這句話的作用來了,當isLoad為false的時候才去後台去資料,取完之後把isLoad改為true,這樣下次點擊的時候 就不會在去取資料了。如果不加這個判斷的話 ,每次點開節點的時候都會重複的添加資料**/if(currentOpenItem.@isLoad == "false"){/*var areaNo:String = currentOpenItem.@areaNo.toString(); this.openLoading(); areaServiceImpl.loadAreaTreeByParentId(areaNo);*/var countryId:String = currentOpenItem.@countryId.toString();if(countryId == "0001"){loadAreaTreeByParentIdHanderTest(this.childTreeStr);}}}}private function loadAreaTreeByParentIdHanderTest(sonXmlTreeStrVar:String):void{var sonXmlTreeStr:XMLList = new XMLList(sonXmlTreeStrVar);if(sonXmlTreeStr != null && sonXmlTreeStr.length() > 0){currentOpenItem.country += sonXmlTreeStr; }else{currentOpenItem.@isBranch = "false";}currentOpenItem.@isLoad = "true";}private function loadAreaTreeByParentIdHander(event:ResultEvent):void{var sonXmlTreeStr:XMLList = new XMLList(event.result.toString());if(sonXmlTreeStr != null && sonXmlTreeStr.length() > 0){//這兩種方法都可以,但是第二種速度快很多//currentOpenItem.appendChild(sonXmlTreeStr); //方法1currentOpenItem.country += sonXmlTreeStr; //方法2}else{currentOpenItem.@isBranch = "false";}//自動擷取樹資料超過顯示範圍時沒有自動產生捲軸.好犀利//(tree.dataProvider as XMLListCollection).itemUpdated(currentOpenItem);//(tree.dataProvider as XMLListCollection).dispatchEvent(new CollectionEvent(CollectionEvent.COLLECTION_CHANGE, false, false, CollectionEventKind.ADD, -1, -1, [currentOpenItem]));currentOpenItem.@isLoad = "true";}]]></mx:Script><mx:Tree id="tree" labelField="@countryName" width="100%" height="100%" showRoot="true" borderStyle="none" borderThickness="1" itemOpen="itemOpenHandler(event)"/><!--樣式--><mx:Style> .errorTip{ color:#FFFFFF; fontSize:14; fontWeight:"bold"; shadowColor: #000000; borderColor: #CE2929; borderStyle: "errorTipRight"; paddingBottom: 4; paddingLeft: 4; paddingRight: 4; paddingTop: 4; } ToolTip { fontSize:14} </mx:Style></mx:Application>
後台程式碼片段
import java.util.ArrayList;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Test {public static void main(String[] args) {String parentTreeStr = loadParenTree();System.out.println(parentTreeStr.toString());String childTreeStr = loadChildTree();System.out.println(childTreeStr.toString());}//擷取第一層:父類層 (實際中可從資料庫動態擷取)public static String loadParenTree() {StringBuffer parentTreeStr = new StringBuffer();parentTreeStr.append("\"<>\"+\n");String[] countryList = new String[]{"中國","美國","英國","法國","德國"};for (int i = 0; i < countryList.length; i++) {parentTreeStr.append("\t\"<country countryId=\'000" + (i+1)+ "\' countryName=\'" + countryList[i]+ "\' isLoad=\'" + "false" //控制載入過就不在載入,預設為沒有載入過+ "\' isBranch=\'" + "true" //控制是否顯示小三角,預設為有三角+ "\' parentId=\'" + "0"+"\'>");parentTreeStr.append("</country>\"+\n");}return parentTreeStr.append("\"</>\";").toString();}//擷取第2層:子類層 (實際中可從資料庫動態擷取)public static String loadChildTree() {StringBuffer parentTreeStr = new StringBuffer();parentTreeStr.append("\"<>\"+\n");String[] countryList = new String[]{"北京市","上海市","廣州市","深圳市","武漢市"};for (int i = 0; i < countryList.length; i++) {parentTreeStr.append("\t\"<country countryId=\'0001000" + (i+1)+ "\' countryName=\'" + countryList[i]+ "\' isLoad=\'" + "false" //控制載入過就不在載入,預設為沒有載入過+ "\' isBranch=\'" + "true" //控制是否顯示小三角,預設為有三角+ "\' parentId=\'" + "0001"+"\'>");parentTreeStr.append("</country>\"+\n");}return parentTreeStr.append("\"</>\";").toString();}}
實際動態擷取資料程式碼片段
public String loadAreaTreeByParentId(String parentId) {StringBuffer areaTreeStr = new StringBuffer();areaTreeStr.append("<>");List<Area> list = hibernateTemplate.getSessionFactory().openSession() .createQuery("from Area p where p.parentAreaNo =:parentAreaNo and p.isVisible ='true' order by p.leafNo") .setString("parentAreaNo", parentId.toString()).list();if(list != null && list.size() > 0){for (Area countryArea : list) {areaTreeStr.append("<country areaNo=\"" + countryArea.getAreaNO() + "\" areaName=\"" + countryArea.getAreaName() + "\" isLoad=\"" + "false" //控制載入過就不在載入,預設為沒有載入過 + "\" isBranch=\"" + "true" //控制是否顯示小三角,預設為有三角 + "\" urls=\"" + countryArea.getUrls() +"\">");areaTreeStr.append("</country>");}return areaTreeStr.append("</>").toString();}else{return "";}}