Extjs表单:加载数据
我想将 json 数据加载到表单中。
我的 json :
{
"success": "true",
"data": {
"operation[id]": "1199",
"operation[startdate]": "2011-10-04 08:00:00",
"operation[starthour]": "08:00",
"operation[enddate]": "2011-10-04 18:00:00",
"operation[endhour]": "18:00",
"operation[year]": "2011",
"operation[abscomment]": "",
"operation[person_id]": "13",
"operation[Mission]": {
"id": "1",
"operation_id": "1199",
"subject": null
}
}
}
它适用于诸如操作[id]之类的键,但不适用于操作[Mission][id]。
在我的表格中:
{
xtype: 'textfield',
fieldLabel: 'Subject',
name:'operation[Mission][subject]',
anchor: '50%',
margin: '15 10 5 10',
allowBlank: false,
blankText:'required'
},
I want to load json data into form.
my json :
{
"success": "true",
"data": {
"operation[id]": "1199",
"operation[startdate]": "2011-10-04 08:00:00",
"operation[starthour]": "08:00",
"operation[enddate]": "2011-10-04 18:00:00",
"operation[endhour]": "18:00",
"operation[year]": "2011",
"operation[abscomment]": "",
"operation[person_id]": "13",
"operation[Mission]": {
"id": "1",
"operation_id": "1199",
"subject": null
}
}
}
It works with key like operation[id] but not with operation[Mission][id].
In my form :
{
xtype: 'textfield',
fieldLabel: 'Subject',
name:'operation[Mission][subject]',
anchor: '50%',
margin: '15 10 5 10',
allowBlank: false,
blankText:'required'
},
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了使操作[Mission][id]有效,您必须更改 JSON 结构并删除一些嵌套。 name 只是一个字符串标识符,您不能使用它来表达如何从嵌套的 JSON 结构中获取数据。
For operation[Mission][id] to be valid, you'd have to change your JSON structure and remove some of the nesting. The name is just a string identifier, you can't use it to express how to get data from a nested JSON structure.
目前,您不能在表单字段定义中使用 name='property.subProperty' :(。
因此,为了实现此目的,我恢复逻辑 - 将(冗余)字段定义添加到模型中:
然后您可以创建一个表单字段如:
它将填充在 form.loadRecord() 上。
在您的情况下,JSON 属性名称有点奇怪,因此映射可能有点困难,所以最好使用转换函数。
Currently you can't use name='property.subProperty' in a form field definition :(.
So in order to make this work, I revert the logic - add (a redundant) field definition to model:
then you can create a form field like:
And it will be populated on form.loadRecord().
In your case the JSON property names are a bit strange, so the mapping might be a bit difficult, so better use a convert function.
或者,您可以使用您可能在其他地方加载的对象中的数据来设置字段的值。
理想情况下,您的模型对象和表单字段的名称匹配,然后您可以在表单上执行 loadRecord 传递您获取的模型记录。但是,如果情况并非如此,并且您有从另一个模型读取的大量数据或只是一个 Ajax 请求,您仍然可以通过设置字段的“value”属性来使用此数据设置表单字段。如果需要,您可以深入研究模型对象的数据属性,如下所示:“myModel.data.subObject.someProperty”
Alternatively you can just set the value of the field with data from an object you may have loaded elsewhere.
Ideally your Model object and Form fields' names match and then you can just do loadRecord on the form passing in your fetched Model record. However, if this is not the case and you have a chunk of data your read from another Model or just an Ajax request you can still set form field with this data by setting "value" property of the field. You can dig into the data property of the Model objects if needed like this: "myModel.data.subObject.someProperty"
如果是 1:1 嵌套关系,一切正常,
但如果你有 1:N 结构:
你如何挖掘数据。
假设您有一个带有一些文本字段和网格(订单详细信息)的表单(订单),这是真实应用程序中众所周知的情况。
您有一个嵌套模型(订单 -> 订单详细信息),并且您想将网格与 OrderDetails 网络数据绑定......这可能吗?
If it is a 1:1 nested relation, everything works fine,
but if you have 1:N structure :
how can you dig into data.
Let's say for example you have a form (orders) with some textfields and a grid (order Details), this is the ever known case in a real app.
You have a nested model (Orders->Order Details) and you want to bind the grid with OrderDetails neted data...is it possible?