Flex 基於資料來源的Menu Tree

來源:互聯網
上載者:User

實現功能: 
1.由外部參數flashvars指定資料來源的檔案位置或render連結.
2.在來源資料上加href和target屬性來控制開啟視窗.
3.可自訂父節點和子節點表徵圖,不設定採用系統預設
.

直接上源碼:

<?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;         }         //菜單表徵圖設定         private function treeIcon(item:Object):Class         {                           var node:XML = XML(item);              trace('icon:' + node.@icon);              var str : String = node.@icon;              //已經設定表徵圖              if(node.hasOwnProperty("@icon"))              {                  if(node.@icon == 'openicon')                  {                        return openicon;                  }                  if(node.@icon == 'closeicon')                  {                        return closeicon;                  }                  if(node.@icon == 'leaficon')                  {                        return leaficon;                  }              }              else              {                //如果沒定義icon就直接用預設的                                         if(!tree1.dataDescriptor.isBranch(item))                  {                      return tree1.getStyle("defaultLeafIcon");                }                if(tree1.isItemOpen(item))                {                      return tree1.getStyle("folderOpenIcon");                }                else                {                    return tree1.getStyle("folderClosedIcon");                }              }              return null;        }                 /**         * 菜單樹單項點擊事件         * */         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                {                    //得到節點對象                    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預設在新視窗中開啟                        openURL(node.@href,"_blank");                    }                }          }                      //頁面跳轉的方法           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>

調用的時候在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" />            <!-- 指定菜單的資料來源 -->            <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='系統管理' icon="openicon">        <node label='使用者管理' icon="closeicon"            href='/main/user/user-list.jsp' target='mainFrame' />        <node label='許可權管理' 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>

源碼下載

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.