简体   繁体   English

将动态JSON数据嵌套到Extjs模型

[英]nested dynamic JSON data to Extjs Model

I want to transform this json to Extjs Model: 我想将此json转换为Extjs模型:

{
  "TableHour": {
    "0": {
      "Rx": 0,
      "Tx": 0
    },
    "1": {
      "Rx": 2,
      "Tx": 0
    },
    "2": {
      "Rx": 0,
      "Tx": 0
    },
    "3": {
      "Rx": 6,
      "Tx": 0
    }
  }
}

I've tried : 我试过了 :

Ext.define("TableHour", {
            extend: 'Ext.data.Model',
        hasMany:  { model:'TableMode' }
    });

    Ext.define("TableMode", {
        extend: 'Ext.data.Model',
        fields: [
        'Rx','Tx'
        ],          
        belongsTo: 'TableHour',
    });     

    var store1 = Ext.create('Ext.data.JsonStore',{
        autoLoad: true,
        model:'TableHour',
        proxy:{
            type:'ajax',
            url:'HoursReports.json',
            reader:{
                type: 'json',
            }
        }

    });   
    console.log(store1.getAt(0));

But the last line, print "undefined". 但是最后一行,打印“ undefined”。 It's sure that model definition is wrong. 确保模型定义是错误的。 The numbers "0" "1" "2" "3" aren't declared in my model beacause they're dynamically generated... how can i do? 我的模型中未声明数字“ 0”,“ 1”,“ 2”,“ 3”,因为它们是动态生成的...我该怎么办?

Your JSON data appears to contain an object with numeric properties as a collection. 您的JSON数据似乎包含一个具有数字属性的对象作为集合。 In order to work with ExtJS this should be an array instead: 为了使用ExtJS,它应该改为一个数组:

{
  "TableHour": [
    {
      "Rx": 0,
      "Tx": 0
    },{
      "Rx": 2,
      "Tx": 0
    },{
      "Rx": 0,
      "Tx": 0
    },{
      "Rx": 6,
      "Tx": 0
    }
  ]
}

Now, if you want to work with a store, then TableHour should be the data root and there should be only one model "TableMode": 现在,如果要使用商店,则TableHour应该是数据根,并且应该只有一个模型“ TableMode”:

Ext.define("TableMode", {
    extend: 'Ext.data.Model',
    fields: [
        'Rx', 'Tx'
    ]
});

var store1 = Ext.create('Ext.data.JsonStore',{
    autoLoad: true,
    model: 'TableMode',
    proxy: {
        type:'ajax',
        url: 'HoursReports.json',
        reader: {
            type: 'json',
            root: 'TableHour'
        }
    }
});

Check out this fiddle for a working example. 查看这个小提琴 ,找到一个可行的示例。

Instead of loading the store automatically using its load () method, you could load the data with Ext.Ajax.request () and transform the data in the success callback such that it has the required format. 您可以使用Ext.Ajax.request()加载数据,并在成功回调中转换数据,使其具有所需的格式,而不是使用其load()方法自动加载存储。 Then you can feed it into your store. 然后,您可以将其送入您的商店。

The data has to look something like this : 数据必须看起来像这样:

{
  "TableHour": [
    {
      "Id": 0,
      "Rx": 0,
      "Tx": 0
    },{
      "Id": 1,
      "Rx": 2,
      "Tx": 0
    },{

I've modified my backend generated Json. 我已经修改了后端生成的Json。 Now my json is: 现在我的json是:

{
  "Date": null,
  "TableHourDto": [
    {
      "Hour": "0",
      "Rx": 3,
      "Tx": 5
    },
    {
      "Hour": "1",
      "Rx": 2,
      "Tx": 0
    },
    {
      "Hour": "2",
      "Rx": 0,
      "Tx": 0
    },
    {
      "Hour": "3",
      "Rx": 5,
      "Tx": 0
    }
}

My Extjs model is: 我的Extjs模型是:

Ext.define("TableHour", {
                extend: 'Ext.data.Model',       
                fields: [
                'Hour','Rx','Tx'
                ],
            });

And my store: 而我的商店:

var store1 = Ext.create('Ext.data.JsonStore',{
                autoLoad:true,
                proxy:{
                    type:'ajax',
                    url:'HoursReports.json',                    
                    reader:{
                        type: 'json',
                        model:'TableHour',
                        root: 'TableHourDto',
                        listeners : {
                            exception: function( reader, response, error, eOpts ){
                                console.log('Got exception');
                            }
                        }
                    }                   
                },
                listeners:{
                    load:function( store, records, successful, eOpts ){
                        console.log(store.getAt(0).data);
                        // This print first: Object {Hour: "0", Rx: 3, Tx: 5}
                    }
                }
            }); 

Having this store, i will build relative Charts... Thank all for reply 有了这家商店,我将建立相关的图表...感谢所有答复

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM