读者根在ExtJS与嵌套的JSON数组 [英] Reader root in ExtJS with nested JSON array
问题描述
我正在尝试使用JSON中的部分数据填充网格。例如(shortnen版本),JSON看起来像这样:
I'm trying to fill the grid with part of data i'm taking from JSON. For example (shortnen version), JSON looks like this:
{
"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
属性,如code> root ='data [0] .devices.disk'或 root ='data.0.devices.disk'
但都没有工作。
我知道最简单的方法是更改JSON响应,但是如果我能够在不更改JSON的情况下填充网格,我感兴趣。
To get info about machine1
's disks i need to get to data[0].devices.disk
, so i thought about changing store.proxy.reader.root
property like root = 'data[0].devices.disk'
or root = 'data.0.devices.disk'
but both didn't work.
I know the easiest way is to change the JSON response, but i'm interested if i am able to fill the grid without changing the JSON.
推荐答案
使用'data [0] .devices.disk'为我工作。您的示例JSON有点混乱,但有一些尾逗号。
Using 'data[0].devices.disk' worked for me. Your example JSON was a little messed up though with some trailing commas.
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
}
});
});
这篇关于读者根在ExtJS与嵌套的JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!