简体   繁体   English

数据数组作为extjs中的模型属性

[英]array of data as model property in extjs

I have a model in extjs like this: 我在extjs中有一个这样的模型:

Ext.define('Game', {
    extend: 'Ext.data.Model',
    fields: [
        { name: 'id', type: 'int' },
        { name: 'Name', type: 'string' },
        //{ name: 'levels' , type:'array'},
    ]
});

I want to use this model for a grid store,but its third property 我想将此模型用于网格存储,但是它的第三个属性

//{ name: 'levels' , type:'array'} // {名称:“ levels”,类型:“ array”}

is an array of data which i want to show them in dynamically created columns of my grid. 是一个数据数组,我想在动态创建的网格列中显示它们。 I think maybe associations on extjs model may bring me a solution but i'm confused how to use them.any solution will be appriciated. 我认为也许extjs模型上的关联可能会给我带来一个解决方案,但是我对如何使用它们感到困惑。任何解决方案都将适用。

Just use the type: 'auto' setting and your array will be untouched. 只需使用以下类型:“自动”设置,您的阵列将保持不变。

Ext.define('Game', {
    extend: 'Ext.data.Model',
    fields: [
        { name: 'id', type: 'int' },
        { name: 'Name', type: 'string' },
        { name: 'levels' , type:'auto'},
    ]
});

And then you could define your columns with renderers like so: 然后可以使用渲染器定义列,如下所示:

columns: [
    {
        text: "ID",
        dataIndex: "id"
    },
    {
        text: "Name",
        dataIndex: "Name"
    },
    {
        text: "Array Item 1",
        dataIndex: "levels",
        renderer: function(value){
             return levels[0];
        }
    },
    {
        text: "Array Item 2",
        dataIndex: "levels",
        renderer: function(value){
             return levels[1];
        }
    }
]

If you wanted to dynamically add columns by items in the array, you would actually have to figure out which record has the array with the most items in it and then add as many columns as you needed to the column array and then set the grid's columns with the .configure method. 如果要按数组中的项目动态添加列,则实际上必须找出哪个记录​​中包含项目最多的数组,然后将所需数量的列添加到列数组中,然后设置网格的列使用.configure方法。 This could probably be done on the store's load event. 这可能是在商店的load事件上完成的。

Supposing this is the data for your model - 假设这是您模型的数据-

{
  "id": 1,
  "name": "game",
  "levels": ["level 1", "level 2", "level 3"]
}

You can always define your model as 您始终可以将模型定义为

Ext.define('Game', {
    extend: 'Ext.data.Model',
    requires: [
        'Ext.data.field.Integer',
        'Ext.data.field.String'
    ],
    fields: [
        { 
            name: 'id', type: 'int' 
        },
        { 
            name: 'Name', type: 'string' 
        },
        { 
            name: 'levels'
        }
    ]
});

Leaving the type empty will be interpreted as "auto" and thus no conversion will be applied to that field. 将类型保留为空将被解释为“自动”,因此不会对该字段应用任何转换。

PS: Mind the trailing commas. PS:请注意结尾的逗号。 :) :)

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

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