将 JSON 数据加载到 ExtJS 数据存储中时遇到问题

2024-01-07

关于如何配置 ExtJS 数据存储来读取传入的 JSON 数据,我已经尝试了所有我能想到的组合。我在以下位置获取 JSON 数据:

[{ "data_type": {"attribute1" : "value1",
                  "attribute2" : "value2",
                  "attribute3" : "value3" 
                  }
 },
 { "data_type": {"attribute1": "value4",
                  "attribute2" : "value5",
                  "attribute3" : "value6" 
                  }
 }
]

我不想解析 JSON 并重新格式化它以使 ExtJS 满意,因为它看起来多余。我最终想要的是一个数据存储,它允许我执行以下操作:

    Ext.create('Ext.container.Container', 
    {
        id: 'junk',
        renderTo: 'slider',
        width: 960,
        height:600,
        layout: 'vbox',
        items: [
           {
              xtype: 'grid',
              title: 'foobar',
              height: 400,
              width: 700,
              store: my_store,
              viewConfig: { emptyText: 'No data' },
              columns: [
                  {
                     text: 'column1',
                     dataIndex: 'attribute1'
                  },{
                     text: 'column2',
                     dataIndex: 'attribute2'
                  },{
                     text: 'column3',
                     dataIndex: 'attribute3'
                  }
              ]
           }
        ]
    }

我知道 ExtJS 知道如何解析这个 JSON,因为我可以这样做:

var foo = Ext.decode(data);
var good_data = foo[0].data_type.attribute1

正如我所期望的那样,它返回“value1”。谁能帮助我理解获取数据模型和存储来做到这一点的神奇咒语?

Thanks!


首先你应该创建模型:

Ext.define('SomeModel', {
    extend: 'Ext.data.Model',
    fields: [
        {name: 'attribute1'},
        {name: 'attribute2'},
        {name: 'attribute3'}
    ]
});

然后您可以通过设置来配置商店以支持您的数据格式record财产给data_type:

var store = Ext.create('Ext.data.Store', {
    autoLoad: true,
    data : data,
    model: 'SomeModel',
    proxy: {
        type: 'memory',
        reader: {
            type: 'json',
            record: 'data_type'
        }
    }
});

工作样本:http://jsfiddle.net/lolo/WfXK6/1/ http://jsfiddle.net/lolo/WfXK6/1/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 JSON 数据加载到 ExtJS 数据存储中时遇到问题 的相关文章

随机推荐