PDA

View Full Version : JSON and DataGrid



same66
23 Dec 2010, 7:31 AM
Hi,

i want to make a grid with extJS and json but, i don't understand how.
I received the json in parameter in my function and in this function I want to build my grid.

My code here :



// my call
this.createGrid(myJson);

// my function
createGrid : function(json){

// here I want initialise my data to set value in my grid...
var reader = new Ext.data.JsonReader({
},[
// ??? :(
]
)
//store
var store = new Ext.data.Store({
reader:reader
});
//loading
store.load();


// create the grid
var grid = new Ext.grid.GridPanel({
store: store,
columns: [
// here create my column
],
width:540,
height:200
});

}thanks :)

dkarso
23 Dec 2010, 7:53 AM
well you should setup the JsonReader properly according to the data you get in function. Say you get this json data



{
results: 2000, // Reader's configured totalProperty
rows: [ // Reader's configured root
// record data objects:
{ id: 1, firstname: 'Bill', occupation: 'Gardener' },
{ id: 2, firstname: 'Ben' , occupation: 'Horticulturalist' },
...
]
}


You should setup JsonReader like so


var reader = new Ext.data.JsonReader({
totalProperty: 'results',
rootProperty: 'rows', //the array where all the items are stored. 1
idProperty: 'id', //the ID of each row
fields: [
{name: 'id'}, {name: 'firstname'}, {name: 'occupation'}
]
});



It all depends on the json data :)