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

本文介绍了一种简单有效的方法,即将服务器返回的JSON数据自动填充到ExtForm组件中的技巧。核心步骤包括:使用json-lib将bean中的值转化为JSON字符串;通过Prototype的evalJSON()方法解析JSON数据;最后调用form的setValues()方法完成数据填充。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

[b]解决的办法是:[/b] :arrow:
(1) 将bean中的值使用json-lib转为json串返回给页面
(2) 使用Prototype提供的String.evalJSON()的方法将json数据装换为对象
[color=blue]备注:
Prototype 1.6 Complete API Reference中Chapter21关于String的evalJSON方法的说明:
evalJSON([sanitize = false]) -> object
Evaluates the JSON in the string and returns the resulting object.[/color]
(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"}
[color=red]说明:bean中的属性名同表单中Field的名称[/color]

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


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

/////////////////////////////////////////////////////////////////////////////
[b]
That is the English version of my note: [/b](sorry about the not nicely words) :wink:

[size=medium][b]How to fill the JSON data into the Ext Form component which from the server?[/b][/size]
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:
[color=red](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[/color]

The method evalJSON is the Prototype 1.6 lib provide.There is the description:
[color=blue]
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.[/color]

That's all, tks
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值