PDA

View Full Version : Adding an additional datastore when using single-file grid definition+data



RoyN
29 Nov 2011, 11:37 AM
Hi Everyone,

I am still getting acquainted with a lot in Ext but moving along so far... I have been using mostly the following example to load the columns and data from a single file - http://www.sencha.com/forum/showthread.php?135873-ExtJs4-autogrid-solution

I think it is most useful because that way I can taylor the view from my script, and send the appropriate columns and data... one question that I have is the following, would it be possible to add an additional datastore with this setup to be able to use cross-lookups with special renderers?

I would like to have something like this:


{"success":true,
"metaData": {
"columns": [
{"text": "ID","dataIndex": 'id',
{"text": "user_id","dataIndex": "user_id", renderer: function(value){
///////my code to lookup data from another store by id...UsersStore.getById(value);
}}}
]
},
"data":[
{ "id": 1, "user_id":123 },
{ "id": 2, "user_id":456 }
],
"users":[
{ "id": 123, "name":"rob" },
{ "id": 456, "name":"john" }
]}


obviously my current grid code must now include this new "users" array/json object...


Ext.require(['Ext.data.*', 'Ext.grid.*']);

Ext.define('Order', {
extend: 'Ext.data.Model',
fields: []
});

Ext.onReady(function(){
var store = new Ext.data.Store({
model: "Order"
});


var store = Ext.create('Ext.data.JsonStore', {
model: 'Order',
proxy: {
type: 'rest',
url: 'viewapi2.php',
reader: {
type: 'json',
root: 'data'
}
}
});
store.load({
callback: function() {
var columns=[];
var data=store.getProxy().getReader().rawData;
if (data.metaData) {
columns = data.metaData.columns;
}
var grid = Ext.create('Ext.grid.Panel', {
id:'usergrid',
renderTo: document.body,
width: 400,
height: 300,
frame: true,
title: 'Users',
store: new Ext.data.Store({
model:"Order"
}),
iconCls: 'icon-user',
columns: columns
});
var jsonData=data.data;
var i=0;
for(i=0;i<jsonData.length;i++){
Ext.getCmp('usergrid').getStore().add(jsonData[i]);
}
}
});
store.load();
});



Thanks a lot for any help

mitchellsimoens
29 Nov 2011, 2:25 PM
You could just merge the objects together in your store load callback.

RoyN
29 Nov 2011, 6:31 PM
Hello Mitchell,

Thank you for trying to help. Nonetheless, as a newbie, I will need a bit more to get by...

I tried setting a variable in the callback function previously, but that wasnt even visible from the renderer callback, so I dont think it would help me...

Basically I want to utilize multiple datastores to do lookups on semi-static data without the need of doing the lookup in my application. Also, the store can also be used to populate form option values, etc...