如何将服务器返回的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