Flex 基于數(shù)據(jù)源的Menu Tree實(shí)現(xiàn)代碼
更新時間:2013年01月30日 17:22:23 作者:
由外部參數(shù)flashvars指定數(shù)據(jù)源的文件位置或render鏈接,在源數(shù)據(jù)上加href和target屬性來控制打開窗口,可自定義父節(jié)點(diǎn)和子節(jié)點(diǎn)圖標(biāo),不設(shè)置采用系統(tǒng)默認(rèn),感興趣的你可以了解下啊,或許對你有所幫助
實(shí)現(xiàn)功能:
1.由外部參數(shù)flashvars指定數(shù)據(jù)源的文件位置或render鏈接.
2.在源數(shù)據(jù)上加href和target屬性來控制打開窗口.
3.可自定義父節(jié)點(diǎn)和子節(jié)點(diǎn)圖標(biāo),不設(shè)置采用系統(tǒng)默認(rèn).
直接上源碼:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
fontFamily="simsun" fontSize="12"
layout="absolute" creationComplete="menu.send();" width="242" height="442" initialize="init()">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.ListEvent;
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
[Bindable]
private var strUrl:String = "TreeMenus.xml";
[Bindable]
private var menus:XML;
[Bindable]
[Embed("open.gif")]
public var openicon:Class;
[Bindable]
[Embed("close.gif")]
public var closeicon:Class;
[Bindable]
[Embed("leaf.gif")]
public var leaficon:Class;
private function init():void
{
this.strUrl = this.parameters.url;
}
private function LoadMenu(event:ResultEvent):void
{
menus = XML(event.result);
var results:XMLList = menus.node;
tree1.dataProvider = results;
}
//菜單圖標(biāo)設(shè)置
private function treeIcon(item:Object):Class
{
var node:XML = XML(item);
trace('icon:' + node.@icon);
var str : String = node.@icon;
//已經(jīng)設(shè)置圖標(biāo)
if(node.hasOwnProperty("@icon"))
{
if(node.@icon == 'openicon')
{
return openicon;
}
if(node.@icon == 'closeicon')
{
return closeicon;
}
if(node.@icon == 'leaficon')
{
return leaficon;
}
}
else
{
//如果沒定義icon就直接用默認(rèn)的
if(!tree1.dataDescriptor.isBranch(item))
{
return tree1.getStyle("defaultLeafIcon");
}
if(tree1.isItemOpen(item))
{
return tree1.getStyle("folderOpenIcon");
}
else
{
return tree1.getStyle("folderClosedIcon");
}
}
return null;
}
/**
* 菜單樹單項(xiàng)點(diǎn)擊事件
* */
private function itemClickHandler(evt:ListEvent):void
{
var item:Object = Tree(evt.currentTarget).selectedItem;
if (tree1.dataDescriptor.isBranch(item))
{
//tree1.expandItem(item, !groupTree.isItemOpen(item), true);
}
else
{
//得到節(jié)點(diǎn)對象
var node:XML = XML(item);
//如果有屬性href
if(node.hasOwnProperty("@href") && node.hasOwnProperty("@target"))
{
openURL(node.@href,node.@target);
}
if(node.hasOwnProperty("@href") && (node.hasOwnProperty("@target") == false))
{
//沒有指定target默認(rèn)在新窗口中打開
openURL(node.@href,"_blank");
}
}
}
//頁面跳轉(zhuǎn)的方法
private function openURL(url:String ,target:String):void
{
var request:URLRequest = new URLRequest(url);
navigateToURL(request,target);
}
]]>
</mx:Script>
<mx:HTTPService url="{strUrl}" id="menu" useProxy="false"
showBusyCursor="true" result="LoadMenu(event)" resultFormat="xml"/>
<mx:Tree iconFunction="treeIcon" id="tree1" width="100%" height="100%" labelField="@label" itemClick="itemClickHandler(event)"/>
</mx:Application>
調(diào)用的時候在flashvars里面加上url=xxx
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
id="tree" width="242" height="442"
codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
<param name="movie" value="${ctx}/js/as/menu.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#869ca7" />
<param name="allowScriptAccess" value="sameDomain" />
<!-- 指定菜單的數(shù)據(jù)源 -->
<param name="flashvars" value="url=${ctx}/user/user!renderMenu.do?id=${user.usid}" />
<embed src="tree.swf" quality="high" bgcolor="#869ca7"
width="242" height="442" name="tree" align="middle"
play="true"
loop="false"
quality="high"
allowScriptAccess="sameDomain"
type="application/x-shockwave-flash"
pluginspage="http://www.adobe.com/go/getflashplayer">
</embed>
</object>
其中url可以指定xml文件的位置或者render的鏈接
示例文件xml:
<?xml version='1.0' encoding='utf-8'?>
<menus>
<node label='系統(tǒng)管理' icon="openicon">
<node label='用戶管理' icon="closeicon"
href='/main/user/user-list.jsp' target='mainFrame' />
<node label='權(quán)限管理' href='/main/user/action-list.jsp'
target='mainFrame' />
<node label='角色管理' href='/main/user/role-list.jsp'
target='mainFrame' />
<node label='域管理' href='/main/user/user-list.jsp'
target='mainFrame' />
<node label='測試'>
<node label='sub folder' href='' target='mainFrame' />
</node>
</node>
<node label='客服'>
<node label='終端信息查詢' href='' target='mainFrame' />
<node label='客服問題-解答記錄' href='' target='mainFrame' />
</node>
</menus>
1.由外部參數(shù)flashvars指定數(shù)據(jù)源的文件位置或render鏈接.
2.在源數(shù)據(jù)上加href和target屬性來控制打開窗口.
3.可自定義父節(jié)點(diǎn)和子節(jié)點(diǎn)圖標(biāo),不設(shè)置采用系統(tǒng)默認(rèn).
直接上源碼:
復(fù)制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
fontFamily="simsun" fontSize="12"
layout="absolute" creationComplete="menu.send();" width="242" height="442" initialize="init()">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.ListEvent;
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
[Bindable]
private var strUrl:String = "TreeMenus.xml";
[Bindable]
private var menus:XML;
[Bindable]
[Embed("open.gif")]
public var openicon:Class;
[Bindable]
[Embed("close.gif")]
public var closeicon:Class;
[Bindable]
[Embed("leaf.gif")]
public var leaficon:Class;
private function init():void
{
this.strUrl = this.parameters.url;
}
private function LoadMenu(event:ResultEvent):void
{
menus = XML(event.result);
var results:XMLList = menus.node;
tree1.dataProvider = results;
}
//菜單圖標(biāo)設(shè)置
private function treeIcon(item:Object):Class
{
var node:XML = XML(item);
trace('icon:' + node.@icon);
var str : String = node.@icon;
//已經(jīng)設(shè)置圖標(biāo)
if(node.hasOwnProperty("@icon"))
{
if(node.@icon == 'openicon')
{
return openicon;
}
if(node.@icon == 'closeicon')
{
return closeicon;
}
if(node.@icon == 'leaficon')
{
return leaficon;
}
}
else
{
//如果沒定義icon就直接用默認(rèn)的
if(!tree1.dataDescriptor.isBranch(item))
{
return tree1.getStyle("defaultLeafIcon");
}
if(tree1.isItemOpen(item))
{
return tree1.getStyle("folderOpenIcon");
}
else
{
return tree1.getStyle("folderClosedIcon");
}
}
return null;
}
/**
* 菜單樹單項(xiàng)點(diǎn)擊事件
* */
private function itemClickHandler(evt:ListEvent):void
{
var item:Object = Tree(evt.currentTarget).selectedItem;
if (tree1.dataDescriptor.isBranch(item))
{
//tree1.expandItem(item, !groupTree.isItemOpen(item), true);
}
else
{
//得到節(jié)點(diǎn)對象
var node:XML = XML(item);
//如果有屬性href
if(node.hasOwnProperty("@href") && node.hasOwnProperty("@target"))
{
openURL(node.@href,node.@target);
}
if(node.hasOwnProperty("@href") && (node.hasOwnProperty("@target") == false))
{
//沒有指定target默認(rèn)在新窗口中打開
openURL(node.@href,"_blank");
}
}
}
//頁面跳轉(zhuǎn)的方法
private function openURL(url:String ,target:String):void
{
var request:URLRequest = new URLRequest(url);
navigateToURL(request,target);
}
]]>
</mx:Script>
<mx:HTTPService url="{strUrl}" id="menu" useProxy="false"
showBusyCursor="true" result="LoadMenu(event)" resultFormat="xml"/>
<mx:Tree iconFunction="treeIcon" id="tree1" width="100%" height="100%" labelField="@label" itemClick="itemClickHandler(event)"/>
</mx:Application>
調(diào)用的時候在flashvars里面加上url=xxx
復(fù)制代碼 代碼如下:
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
id="tree" width="242" height="442"
codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
<param name="movie" value="${ctx}/js/as/menu.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#869ca7" />
<param name="allowScriptAccess" value="sameDomain" />
<!-- 指定菜單的數(shù)據(jù)源 -->
<param name="flashvars" value="url=${ctx}/user/user!renderMenu.do?id=${user.usid}" />
<embed src="tree.swf" quality="high" bgcolor="#869ca7"
width="242" height="442" name="tree" align="middle"
play="true"
loop="false"
quality="high"
allowScriptAccess="sameDomain"
type="application/x-shockwave-flash"
pluginspage="http://www.adobe.com/go/getflashplayer">
</embed>
</object>
其中url可以指定xml文件的位置或者render的鏈接
示例文件xml:
<?xml version='1.0' encoding='utf-8'?>
<menus>
<node label='系統(tǒng)管理' icon="openicon">
<node label='用戶管理' icon="closeicon"
href='/main/user/user-list.jsp' target='mainFrame' />
<node label='權(quán)限管理' href='/main/user/action-list.jsp'
target='mainFrame' />
<node label='角色管理' href='/main/user/role-list.jsp'
target='mainFrame' />
<node label='域管理' href='/main/user/user-list.jsp'
target='mainFrame' />
<node label='測試'>
<node label='sub folder' href='' target='mainFrame' />
</node>
</node>
<node label='客服'>
<node label='終端信息查詢' href='' target='mainFrame' />
<node label='客服問題-解答記錄' href='' target='mainFrame' />
</node>
</menus>
相關(guān)文章
Flex中給按鈕添加鏈接點(diǎn)擊鏈接打開網(wǎng)頁的方法
我們需要這樣的一個效果:點(diǎn)擊鏈接打開一個網(wǎng)頁。下面為大家介紹下Flex中如何給一個按鈕添加鏈接實(shí)現(xiàn)點(diǎn)擊打開網(wǎng)頁,感興趣的朋友可以參考下2013-12-12
Flex中對表格中某列的值進(jìn)行數(shù)字格式化保留兩位小數(shù)
表格中展示的比率,對比率的處理是:保留兩位小數(shù),并向上保留。通過對某列的值進(jìn)行數(shù)字格式化來實(shí)現(xiàn)保留兩位小數(shù)2014-10-10
flex利用webservice上傳照片實(shí)現(xiàn)代碼
這篇文章主要介紹了flex利用webservice上傳照片實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-05-05
Flex中如何動態(tài)生成DataGrid以及動態(tài)生成表頭
因某些需要,DataGrid及其表頭需要動態(tài)生成,網(wǎng)上的解決方案打多籠統(tǒng),下面有個不錯的解決方法,感興趣的朋友可以參考下2013-10-10
Flex動態(tài)生成可編輯的DataGrid具體實(shí)現(xiàn)代碼
DataGrid具有以下功能:表頭是動態(tài)生成的、每行都是有序號的、每行都是可以編輯、插入、刪除、修改,接下來為大家分享下Flex如何動態(tài)生成可編輯的DataGrid2013-04-04
Flex中通過RadioButton進(jìn)行切換示例代碼
這篇文章主要介紹了Flex中通過RadioButton進(jìn)行切換示例代碼,需要的朋友可以參考下2014-02-02

