PDA

View Full Version : What should be the jsonstore definition



vijay4freedom
9 Feb 2011, 9:47 PM
Hi,

If following is the json data from server, what should be the jsonstore definition?


{"list":
[{"name":"james","age":"34","kids":[{"name":"john","age":"4"},{"name":"sam","age":"2"}]},
{"name":"andreah","age":"30","kids":[{"name":"rose","age":"3"},{"name":"reese","age":"2"}]}
]}

var store = new CQ.Ext.data.JsonStore({
root: 'list',
fields: [{name:'name', type:'string'},
{name:'age'},
{name:'kids',type:'????'}
]
});

Barzoy
9 Feb 2011, 11:25 PM
First, define a grid record class. It always useful to have it for data insertion, editing, loading into form etc

var GridRecordClass = Ext.data.Record.create([
{name: 'name'},
{name: 'age', type: Ext.data.Types.INTEGER},
{name: 'kids'}
]);

Pass that class to DataReader config

var dataReader = new Ext.data.JsonReader({
root: 'list',
fields: GridRecordClass
});

Write renderer function to dislay array of kids inside grid cells

var colModel = new Ext.grid.ColumnModel([{
id: 'name',
dataIndex: 'name',
header: 'Name',
tooltip: 'Persons name',
width: 200
},{
id: 'age',
dataIndex: 'age',
header: 'Age',
tooltip: 'Persons age (years)',
width: 50
},{
id: 'kids',
dataIndex: 'kids',
header: 'Kids',
width: 150,
renderer: function(value, meta, record, rowIndex, colIndex, store) {
var result = [];
var total = 0;
if (Ext.isArray(value)) {
Ext.each(value, function(kid) {
result.push(kid.name);
total++;
});
result = result.join(', ');
}
return (total == 0) ? 'none' : String.format('Person has {0} kids, here they are: {1}', total, result);
}
}]);

Finally, pass that DataReader and ColModel to your GridPanel config

vijay4freedom
10 Feb 2011, 2:22 AM
@Barzoy
Thank you. It worked.:)