将JSON数据加载到ExtJS数据存储区中 [英] Trouble loading JSON data into a ExtJS datastore

查看:170
本文介绍了将JSON数据加载到ExtJS数据存储区中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了如何配置我的ExtJS数据存储来读取我的传入JSON数据的每一个组合。我得到这个JSON数据:

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

我不想解析JSON并重新格式化,以使ExtJS快乐,因为它似乎是多余的。我想要的是一个数据存储区,它允许我做:

  Ext.create('Ext.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'正如我所料。任何人都可以帮助我理解获得数据模型和存储的神奇咒语吗?



谢谢!

解决方案

首先你应该创建模型:

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

然后您可以通过设置记录来配置商店来支持您的数据格式code> property to 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/


I've tried every combination I can think of in terms of how to configure my ExtJS datastore to read my incoming JSON data. I'm getting this JSON data in:

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

I don't want to parse the JSON and reformat it in order to make ExtJS happy because it seems redundant. What I want to end up with is a datastore which would allow me to do:

    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'
                  }
              ]
           }
        ]
    }

I know ExtJS knows how to parse this JSON, because I can do:

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

And that returns 'value1' as I would expect. Can anyone help me understand the magical incantation to get a datamodel and store to do that?

Thanks!

解决方案

First of all you should create model:

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

Then you can configure store to support your data format by setting record property to data_type:

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

Working sample: http://jsfiddle.net/lolo/WfXK6/1/

这篇关于将JSON数据加载到ExtJS数据存储区中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆