`

如何将服务器返回的Json值填入到EXT的Form中 (双语版)

阅读更多
如何将服务器返回的json数据自动回填到我的form里头去?想必使用Ext Form组件的开发者都会遇到这样的问题。

解决的办法是:
(1) 将bean中的值使用json-lib转为json串返回给页面
(2) 使用Prototype提供的String.evalJSON()的方法将json数据装换为对象
备注:
Prototype 1.6 Complete API Reference中Chapter21关于String的evalJSON方法的说明:
evalJSON([sanitize = false]) -> object
Evaluates the JSON in the string and returns the resulting object.

(3) 调用form的setValues()方法将转换后的json对象填入到表单中即可

jsondata中的数据为
{"addAbility":"0","dataFilter":"level_type > 1","dataFilterDesc":"fsa as","deleteSql":"delete from BANK_LEVEL","description":"特斯塔","extractSql":"select * from BANK_LEVEL","insertSql":"delete from BANK_LEVEL","tableAlias":"机构级别","tableName":"BANK_LEVEL","type":"1","updateSql":"delete from BANK_LEVEL","userFilter":"sadf  ","userFilterDesc":"as df"}
说明:bean中的属性名同表单中Field的名称

var data = jsondata.evalJSON();
formPanel.getForm().setValues(data);


此方法代码只需两行,比较light。

/////////////////////////////////////////////////////////////////////////////

That is the English version of my note:
(sorry about the not nicely words) 

How to fill the JSON data into the Ext Form component which from the server?
maybe all the base Ext developer face to this question.

Resolve:

The core of the code is only two lines.
var data = jsondata.evalJSON();
formPanel.getForm().setValues(data);


Explain:
(1) jsondata is the Server response data, and via the evalJSON method that transform it to the JavaScript Object.
(2) then call ur formPanel.getForm() to get the form,and use the form's setValues() method set the javabean's field value into the form's from the transform object


The method evalJSON is the Prototype 1.6 lib provide.There is the description:

Prototype 1.6 Complete API Reference Chapter21 about String's evalJSON method:
evalJSON([sanitize = false]) -> object
Evaluates the JSON in the string and returns the resulting object.


That's all, tks



1
0
分享到:
评论
2 楼 Seraph115 2010-01-15  
sunjilu05 写道
将bean中的值使用json-lib转为json串返回给页面
=========
如果你的bean的属性是另外一个对象,也就是说转换出来的JSON是有对象的嵌套的,类似于:
{"addAbility":"0","dataFilter":"level_type > 1","userFilterDesc":"as",
child:{
    "name":"dddd",
    "foo":"bbb"
}
}

有解决方案吗


Json-lib是支持嵌套解析的,不过是要分是单一对象还是数组对象

数组对象: JSONArray jsonArray = JSONArray.fromObject(obj);
单一对象: JSONObject jsonObject = JSONObject.fromObject(obj);


1 楼 sunjilu05 2010-01-15  
将bean中的值使用json-lib转为json串返回给页面
=========
如果你的bean的属性是另外一个对象,也就是说转换出来的JSON是有对象的嵌套的,类似于:
{"addAbility":"0","dataFilter":"level_type > 1","userFilterDesc":"as",
child:{
    "name":"dddd",
    "foo":"bbb"
}
}

有解决方案吗

相关推荐

Global site tag (gtag.js) - Google Analytics