程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> TreeLoader擴展,支持josn-plugin返回的json對象中包含的數組值

TreeLoader擴展,支持josn-plugin返回的json對象中包含的數組值

編輯:關於JAVA

Struts2的josn插件 josn-plugin 對我們開發extjs程序很方便,但是他每次返回的數據格式都是以一個對象的方式返回的,對象中可以包含其他格式的數據,比如數組.

這就是一般的返回格式

Js代碼

{"root":[{"address":"shenzhen","company":"Bank of China","manager":"true","name":"Jack","password":"a","userid":"8ad08c8323eeb3ba0123eeb3c6e20001","username":"a","zip":"518000"},{"address":"dongjing","company":"Bank of Japan","manager":"false","name":"Jack Cheng","password":"b","userid":"8ad08c8323eeb3ba0123eeb3c6e20002","username":"b","zip":"518000"}],"totalProperty":2}

struts2後台配置為

Xml代碼

<action name="getUsers" class="userAction" method="getUsers">
   <result type="json">
     <param name="root">
     page
     </param>
     <param name="excludeNullProperties">
     true
     </param>
     <param name="includeProperties">
     root.*,totalProperty 
     </param>
   </result>
</action>

而我們做樹(TreePanel)的時候需要服務器返回的數據是數組對象的形式.如下

Js代碼

[{"address":"shenzhen","company":"Bank of China","manager":"true","name":"Jack","password":"a","userid":"8ad08c8323eeb3ba0123eeb3c6e20001","username":"a","zip":"518000"},{"address":"dongjing","company":"Bank of Japan","manager":"false","name":"Jack Cheng","password":"b","userid":"8ad08c8323eeb3ba0123eeb3c6e20002","username":"b","zip":"518000"}]

但是json-plugin不支持直接返回這種格式,所以我們可以修改一下TreeLoader的處理函數

Js代碼

//TreeLoader擴展,支持josn-plugin返回的json對象中包含的數組值
Ext.tree.JsonPluginTreeLoader = function (config) {
   this.rootName = 'root';
   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 || 'root'];
       }
//--------------------------------------------------
       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);
     }
   }
});

使用方法

Js代碼

{
   xtype: 'treepanel',
   loader: new Ext.tree.JsonPluginTreeLoader({
     dataUrl: 'xxx.action'
     //rootName:'nodes' //這裡可以動態配置,已配合服務器返回的數組名稱:)
   }),
   root: new Ext.tree.AsyncTreeNode({
     text: '根節點'
   })
}

後台struts2的josn配置如下

Xml代碼

<action name="getUsers" class="userAction" method="getUsers">
     <result type="json">
         <param name="root">
         page
         </param>
         <param name="excludeNullProperties">
         true
         </param>
         <param name="includeProperties">
         root.*
         </param>
     </result>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved