TreeLoader扩大,支持josn-plugin返回的json对象中包含的数组值[Java编程]
本文“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);
}
}
});
以上是“TreeLoader扩大,支持josn-plugin返回的json对象中包含的数组值[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |