Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

array of data as model property in extjs

I have a model in extjs like this:

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

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.

like image 936
Yazdan Bayati Avatar asked Mar 27 '26 11:03

Yazdan Bayati


1 Answers

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

like image 73
Reimius Avatar answered Mar 30 '26 02:03

Reimius