例子一
例子二
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import flash.utils.Dictionary;
import mx.events.TreeEvent;
import mx.rpc.AsyncToken;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
private var dictionary:Dictionary = new Dictionary();
private function itemOpenHandler(event:TreeEvent):void
{
var item:Object = event.item;
if (item && item.@loadFlag == "false")
{
service.url = item.@url;
var token:AsyncToken = service.send();
dictionary[token] = item;
}
}
private function resultHandler(event:ResultEvent):void
{
var channel:XML = XML(dictionary[event.token]);
var result:XML = XML(event.result);
var items:XMLList = result..item;
var item:XML = null;
var node:XML = null;
channel.@loadFlag = "true";
delete channel.*;
for each (item in items)
{
node = <node/>;
node.@label = item.title;
channel.appendChild(node);
}
}
private function faultHandler(event:FaultEvent):void
{
var channel:XML = XML(dictionary[event.token]);
channel.@loadFlag = "true";
delete channel.*;
}
private function getLabel(item:Object):String
{
var node:XML = XML(item);
if (node.name().toString() == "item")
return node.title;
else
return node.@label;
}
]]>
</mx:Script>
<mx:XML id="blog">
<root label="新浪博客">
<channel label="文化" url="http://rss.sina.com.cn/blog/index/cul.xml" loadFlag="false">
<node label="加载中..." />
</channel>
<channel label="八卦" url="http://rss.sina.com.cn/blog/index/ent.xml" loadFlag="false">
<node label="加载中..." />
</channel>
<channel label="情感" url="http://rss.sina.com.cn/blog/index/feel.xml" loadFlag="false">
<node label="加载中..." />
</channel>
<channel label="观点" url="http://rss.sina.com.cn/blog/index/other.xml" loadFlag="false">
<node label="加载中..." />
</channel>
<channel label="财经" url="http://rss.sina.com.cn/blog/index/stocks.xml" loadFlag="false">
<node label="加载中..." />
</channel>
</root>
</mx:XML>
<mx:HTTPService id="service" concurrency="multiple" resultFormat="xml"
result="resultHandler(event)" fault="faultHandler(event)" />
<mx:Tree id="tree" width="400" height="500"
dataProvider="{blog}" labelField="@label"
itemOpen="itemOpenHandler(event)" />
</mx:Application>
例子二
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%"
backgroundColor="#FFFFFF" creationComplete="vsstree.send();initEvent()">
<mx:HTTPService id="vsstree" showBusyCursor="true" method="POST" url="xml/tree.xml" useProxy="false" result="httpResult(event)" resultFormat="e4x">
</mx:HTTPService>
<mx:Script>
<![CDATA[
import action.myevent.EventNames;
import action.myevent.CustomEvent;
import mx.modules.IModuleInfo;
import mx.modules.Module;
import mx.modules.ModuleManager;
import model.loginsuccess;
import mx.core.Application;
import mx.controls.Text;
import mx.events.TreeEvent;
import mx.collections.ArrayCollection;
import mx.collections.ICollectionView;
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
[Bindable]
private var listtree:XML;
private var selectedNode:XML;
private var catalogsid:String;
private function httpResult(event:ResultEvent):void
{
listtree= event.result as XML;
}
//点击树触发事件:1、右侧显示当前目录人员权限情况。2、可能要找出下级目录
private function treeChange(e:Event):void{
selectedNode = Tree(e.target).selectedItem as XML;
catalogsid = selectedNode.attribute("id");
CustomEvent.dispatcher.dispatchEvent(new CustomEvent(EventNames.CATALOGROW,catalogsid));
}
//点击树项目取到其下一级子目录
private function initEvent():void{
CustomEvent.dispatcher.addEventListener(EventNames.CATALOGROW,gettree);
}
private function gettree(e:CustomEvent):void{
catalogsid = e.data as String;
remotetree.getTree(catalogsid);
}
private function getResult(e:ResultEvent):void{
//得到后台返回的XML字符串
var str:String = e.result as String;
if(str!=null&&str!=""){
var child:XML = new XML(str);
/**
* 没点击过的需要appendChild,否则不需要。不然每点一次都会增加子目录
* ??????????????碰到的问题:selectedNode.appendChild(child)增加的时候本级目录下面会多出一级(跟本级目录一样)!!!
* ??????????????因为后台返回的是一个符合XML对象的字符串,一定要包含根节点(就是本级目录),要想办法把这个本级目录去掉或者不显示
* >>>>>>>>>>>>>>解决的办法:将返回的XML对象加在本级目录的父级目录上,再把本级目录删掉就OK了
* 名词解释:本级目录-->点击的目录,目录-->节点
*/
if(selectedNode.children()==null||selectedNode.children().length()==0){
//点击不是root目录的目录
if(selectedNode.parent()!=null){
/**
* 在本级目录的上级添加后台返回的数据,此时效果相当于本级目录的同级增加了一个目录(后台返回的数据)
* addChildAt参数说明:
* 1、要增加子目录的位置,即子目录放在哪个目录下面 类型:Object
* 2、要增加的子目录 类型:XML
* 3、增加的子目录在父目录中的位置索引 类型:int
* 4、包含此节点的整个集合 类型:Object
*/
mytree.dataDescriptor.addChildAt(selectedNode.parent(),child,selectedNode.childIndex(),null);
//把点击的目录删掉
delete selectedNode.parent().children()[selectedNode.childIndex()];
}else{
//点击root目录,直接appendChild然后不显示root就达到我们想要的效果了
selectedNode.appendChild(child);
mytree.showRoot = false;
}
}
}
}
//树上显示其有几个子目录
private function tree_labelFunc(item:XML):String {
var children:ICollectionView;
var suffix:String = "";
if (mytree.dataDescriptor.isBranch(item)) {
children = mytree.dataDescriptor.getChildren(item);
suffix = " (" + children.length + ")";
}
return item[mytree.labelField] + suffix;
}
]]>
</mx:Script>
<mx:RemoteObject id="remotetree" destination="Tree" result="getResult(event)"/>
<mx:Canvas dropShadowEnabled="false" width="100%" height="100%">
<mx:Tree id="mytree" width="100%" height="100%"
dataProvider="{listtree}"
change="treeChange(event)"
labelField="@label"
labelFunction="tree_labelFunc"
folderClosedIcon="@Embed('assets/image/foldericon.gif')"
folderOpenIcon="@Embed('assets/image/openfoldericon.gif')"
defaultLeafIcon="@Embed('assets/image/foldericon.gif')"/>
</mx:Canvas>
</mx:Canvas>