日期:2014-05-16  浏览次数:20400 次

ext2.2以上扩展TreeLoader 支持Struts2通过jsonplugin返回json对象
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib  uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title><%=(String)request.getSession().getAttribute("title")%></title>
		<link rel="stylesheet" href="css/icons.css" type="text/css"></link>
		<link rel="stylesheet" href="css/code.css" type="text/css"></link>
		<link rel="stylesheet" type="text/css"
			href="<%=request.getContextPath()%>/resources/css/ext-all.css" />
		<script type="text/javascript"
			src="<%=request.getContextPath()%>/js/adapter/ext/ext-base.js"></script>
		<script type="text/javascript"
			src="<%=request.getContextPath()%>/js/ext-all.js"></script>
		<script type="text/javascript"
			src="<%=request.getContextPath()%>/js/ext-lang-zh_CN.js"></script>
		<script type="text/javascript" src="<%=request.getContextPath()%>/js/jinyongtab.js"></script>
		<script>
   var Uname='';
	Ext.onReady(function() {  	   
	       Ext.QuickTips.init();	
	       
	       //TreeLoader扩展,支持josn-plugin返回的json对象中包含的数组值
	       Ext.tree.JsonPluginTreeLoader = function (config) {
	          this.rootName = 'nodes';
	          Ext.tree.JsonPluginTreeLoader.superclass.constructor.call(this, config);
	       }
	       Ext.extend(Ext.tree.JsonPluginTreeLoader, Ext.tree.TreeLoader, {
 	         processResponse: function (response, node, callback) {
   	         var json = response.responseText;
   	         try {
    	          var o = response.responseData || Ext.decode(json);
	              //在原代码基础上增加了下面处理---------------------
  	            if (Ext.type(o) == 'object') {//如果返回的是对象则获取他的root部分,rootName是可以在使用的时候配置的
   	             o = o[this.rootName || 'nodes'];
   	           }
	              //--------------------------------------------------
  	            node.beginUpdate();
  	            for (var i = 0, len = o.length; i < len; i++) {
   	             var n = this.createNode(o[i]);
  	              if (n) {
  	                node.appendChild(n);
  	              }
  	            }
  	            node.endUpdate();
  	            this.runCallback(callback, scope || node, [node]);
  	          } catch(e) {
  	            this.handleFailure(response);
 	           }
 	         }
	       });           
	       //end***************
	         		   
			// tree start
			var tree = new Ext.tree.TreePanel( {				
				title : '树',
				el:'win',			
				region : 'west',
				split : true,
				border : true,
				width : 220,
				minSize : 100,
				maxSize : 300,						
				loader: new Ext.tree.JsonPluginTreeLoader({dataUrl: 'loadTree.action'})		
				//new Ext.tree.TreeLoader({dataUrl:'Data.txt'})
			});
			var root = new Ext.tree.AsyncTreeNode( {
				text : '功能菜单'
			});	
			tree.setRootNode(root); 
			root.expand();	
			tree.render(); 
			
			 			
			//树节点监控事件  点击树叶子节点触发 点击跟则将之展开		
			tree.on('click',function(node, event){	
			   event.stopEvent();   		      
               if(node.isLeaf()){               
                  // var templink = node.attributes.link;                  
                   var n = contentPanel.getComponent(node.id); 
                   //alert(node.id);                          
                   if (!n) { //判断是否已经打开该面板    
                        n = contentPanel.add({      
                           'id':node.id,      
                           'title':node.text,                                
                            closable:true, 
                            //autoLoad:{url:"iframe.jsp", scripts:true}   //通过autoLoad属性载