我正在尝试使用JSON中的部分数据来填充网格。例如(shortnen版本),JSON看起来像这样:
{
"data": [
{
"name": "machine1",
"devices": {
"disk": [
{
"type": "file",
"device": "disk",
},
{
"type": "block",
"device": "cdrom",
}
],
},
},
{
"name": "machine2",
"devices": {
"disk": [
{
"type": "file",
"device": "disk",
},
{
"type": "block",
"device": "cdrom",
}
],
},
]
}
要获取有关machine1
我需要访问data[0].devices.disk
的磁盘的信息,所以我考虑更改store.proxy.reader.root
属性,例如root = 'data[0].devices.disk'
或root = 'data.0.devices.disk'
但是两者都没用。
我知道最简单的方法是更改JSON响应,但我很感兴趣,如果我能够在不更改JSON的情况下填充网格。
答案 0 :(得分:2)
使用'data [0] .devices.disk'为我工作。你的示例JSON虽然有一些尾随的逗号,但有点混乱。
Ext.define('User', {
extend: 'Ext.data.Model',
fields: ['type', 'device']
});
Ext.onReady(function() {
var myData = '{"data":[{"name":"machine1","devices":{"disk":[{"type":"file","device":"disk"},{"type":"block","device":"cdrom"}]}},{"name":"machine2","devices":{"disk":[{"type":"file","device":"disk"},{"type":"block","device":"cdrom"}]}}]}';
var store = Ext.create('Ext.data.Store', {
model: 'User',
data: Ext.decode(myData),
proxy: {
type:'memory',
reader: {
type:'json',
root: 'data[0].devices.disk'
}
}
});
Ext.create('Ext.grid.Panel', {
store: store,
stateful: true,
collapsible: true,
multiSelect: true,
stateId: 'stateGrid',
columns: [
{
text : 'type',
dataIndex: 'type'
},
{
text : 'device',
dataIndex: 'device'
}
],
height: 350,
width: 600,
title: 'Array Grid',
renderTo: 'grid',
viewConfig: {
stripeRows: true,
enableTextSelection: true
}
});
});