Extjs表单:加载数据

发布于 2024-12-08 06:04:22 字数 840 浏览 1 评论 0原文

我想将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

↙温凉少女 2024-12-15 06:04:22

为了使操作[Mission][id]有效,您必须更改 JSON 结构并删除一些嵌套。 name 只是一个字符串标识符,您不能使用它来表达如何从嵌套的 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[Mission][operation_id]":"1199",
        "operation[Mission][subject]":null
    }
}

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.

{
    "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[Mission][operation_id]":"1199",
        "operation[Mission][subject]":null
    }
}
み零 2024-12-15 06:04:22

目前,您不能在表单字段定义中使用 name='property.subProperty' :(。

因此,为了实现此目的,我恢复逻辑 - 将(冗余)字段定义添加到模型中:

Ext.define('operation', {
  extend: 'Ext.data.Model',
  fields: [
    'id',
    'startDate',
    'endDate',
    ...
    {name: 'missionId', mapping: 'mission.id'}
  ]
});

然后您可以创建一个表单字段如:

{
  xtype: 'displayfield',
  name: 'missionId',
  ...
}

它将填充在 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:

Ext.define('operation', {
  extend: 'Ext.data.Model',
  fields: [
    'id',
    'startDate',
    'endDate',
    ...
    {name: 'missionId', mapping: 'mission.id'}
  ]
});

then you can create a form field like:

{
  xtype: 'displayfield',
  name: 'missionId',
  ...
}

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.

绳情 2024-12-15 06:04:22

或者,您可以使用您可能在其他地方加载的对象中的数据来设置字段的值。


理想情况下,您的模型对象和表单字段的名称匹配,然后您可以在表单上执行 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"

撩动你心 2024-12-15 06:04:22

如果是 1:1 嵌套关系,一切正常,
但如果你有 1:N 结构:

 "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[Mission][operation_id]":"1199",
        "operation[Mission][subject]":null,
         "operation[Mission][id]":"2",
        "operation[Mission][operation_id]":"1123",
        "operation[Mission][subject]":"Second nested subject"

你如何挖掘数据。
假设您有一个带有一些文本字段和网格(订单详细信息)的表单(订单),这是真实应用程序中众所周知的情况。
您有一个嵌套模型(订单 -> 订单详细信息),并且您想将网格与 OrderDetails 网络数据绑定......这可能吗?

If it is a 1:1 nested relation, everything works fine,
but if you have 1:N structure :

 "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[Mission][operation_id]":"1199",
        "operation[Mission][subject]":null,
         "operation[Mission][id]":"2",
        "operation[Mission][operation_id]":"1123",
        "operation[Mission][subject]":"Second nested subject"

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文