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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15201099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 23:51:19  来源:igfitidea点击:

array of data as model property in extjs

javascriptextjsextjs4

提问by Yazdan Bayati

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

//{ name: 'levels' , type:'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 模型上的关联可能会给我带来一个解决方案,但我很困惑如何使用它们。任何解决方案都会被应用。

回答by Reimius

Just use the type: 'auto' setting and your array will be untouched.

只需使用 type: 'auto' 设置,您的数组将保持不变。

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. This could probably be done on the store's load event.

如果您想按数组中的项目动态添加列,您实际上必须确定哪条记录具有包含最多项目的数组,然后根据需要向列数组添加尽可能多的列,然后设置网格的列使用 .configure 方法。这可能可以在商店的加载事件上完成。

回答by Yellen

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:注意尾随逗号。:)