加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

flex树动态加载

发布时间:2020-12-15 03:58:06 所属栏目:百科 来源:网络整理
导读:背景:一次加载所有的数据,性能非常低下,所以默认只加载第一层,当点击的时候再去加载下面的孩子数据 ? ? ? ? ? ? ? ? ? ? ? ? ?? 点击后 ---------------------------- ? ? ? ? ?xml version="1.0" encoding="utf-8"?mx:Application xmlns:mx="http://www

背景:一次加载所有的数据,性能非常低下,所以默认只加载第一层,当点击的时候再去加载下面的孩子数据

? ? ? ? ? ? ? ? ? ? ? ? ??

点击后 ----------------------------> ? ? ? ?

<?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); //方法1
					currentOpenItem.country += sonXmlTreeStr;  //方法2
				}else{
					currentOpenItem.@isBranch = "false";
				}
				
				//自动获取树数据超过显示范围时没有自动生成滚动条.好犀利
				//(tree.dataProvider as XMLListCollection).itemUpdated(currentOpenItem);
				//(tree.dataProvider as XMLListCollection).dispatchEvent(new CollectionEvent(CollectionEvent.COLLECTION_CHANGE,false,CollectionEventKind.ADD,-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 "";
		}
	}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读