开发者

How to get access to the json label as well as the value in an ExtJS grid?

开发者 https://www.devze.com 2023-03-11 08:14 出处:网络
I would like to have every value of a record populated in a grid for viewing but have no idea how to do that.

I would like to have every value of a record populated in a grid for viewing but have no idea how to do that.

For example if the JSON looks like below I want to be able to have firstName, lastName and age in the first column and the associated values in corresponding rows in the second column.

{
   "people": [
 开发者_StackOverflow       {"firstName": "Jane",
        "lastName": "Doe",
        "age": 23}
    ]
}

Any thoughts are greatly appreciated!


Well, first you need to set columns to your grid:

columns: [
                {header: 'firstName',  dataIndex: 'firstName',flex:1},
                {header: 'lastName',   dataIndex: 'lastName',  flex:1},
                {header: 'age',        dataIndex: 'age',flex:1}
        ],

then you need to define a model and a store:

Ext.define('gridModel', {
extend: 'Ext.data.Model',
fields: [
   {name: 'firstName', type:'string'},
   {name: 'lastName', type:'int'},
   {name: 'age', type:'string'}
]

});

var myStore = Ext.create('Ext.data.Store', {
model: 'gridModel',
proxy: {
    type: 'ajax',
    url : 'url of your json',
    reader:{ 
        type:'json',
        root: 'people'          
    }
},
autoLoad:true   

});

and your data should be in View. And define your store in grid: (store: myStore)

Cheers!

0

精彩评论

暂无评论...
验证码 换一张
取 消