PDA

View Full Version : Creating records for a JsonReader Dynamically



jdurante
24 Apr 2009, 6:32 AM
Hello,

I am trying to create the records to be read by my JsonReader dynamically. Currently, I am creating a Store to get the information about my Grid Columns, and creating a ColumnModel from it, this works properly.

Since the data that will populate the Grid should use the same dataIndexes, I am trying to create the Ext.data.Record using the column store.

Here is the code that creates the column Store



var columnStore = new Ext.data.Store({
reader: new Ext.data.JsonReader({
root: 'columns'
}, columnMeta),
proxy: new Ext.data.HttpProxy({
url: '/oma/orderQueue.php',
method: 'POST'
}),
baseParams: {'action':'getQueueColumns', 'viewName':viewName, 'userGUID':userGUID},
listeners: {
'load': function () {
var index = 1;
columns[0] = sm;
columnStore.each(function(r) {
hiddenBool = false;
if(r.data['IsHidden'] == 1) { hiddenBool = true; }
if(r.data['Width'] == 'NULL') { r.data['Width'] = 100; }
if(r.data['DataIndex'] == 'date') { r.data['Renderer'] = Ext.util.Format.dateRenderer('M j Y g:ia'); }

columns[index] = {
columnID: r.data['ColumnID'],
header: r.data['Header'],
dataIndex: r.data['DataIndex'],
renderer: r.data['Renderer'],
hidden: hiddenBool,
width: parseInt(r.data['Width']),
id: index
};
fieldArray[index-1] = { name: r.data['DataIndex'] };
index++;
});
gridColModel.defaultSortable = true;
gridColModel.setConfig (columns);
}
}
});
As you can see, I create a fieldArray of the dataIndexes. I then create an Ext.data.Record with that array.



var fnames = Ext.data.Record.create(fieldArray);
Here is my Grid Data Store Code:



var orderStore = new Ext.data.Store({
reader: new Ext.data.JsonReader({
root: 'orders'
}, fnames),
proxy: new Ext.data.HttpProxy({
url: '/oma/orderQueue.php',
timeout: 60000
}),
baseParams:{'userGUID':userGUID, 'action':'getOrderQueue', 'viewName':viewName, 'marketerID':marketerID}
});
So, I'm telling the orderStore to use the fnames Records, which I would think should work, but it does not. There are only 2 rows that I am loading. When the Grid renders, it creates a row for each, but there are no cells.

When I look at the DOM, under the orderStore > reader > jsonData, all of the data is there and correct.

I'm really lost at what I am doing incorrectly, and any help would be appreciated.

Thanks!

Jon

Animal
24 Apr 2009, 6:37 AM
columnMeta is your Record constructor isn't it?

If not, it should be. Then you just new that.

Animal
24 Apr 2009, 6:39 AM
Even if you don't create your own Record constructor, the Reader always creates one if you send an Array in as that second param.

You can access it: http://extjs.com/deploy/dev/docs/?class=Ext.data.Store&member=recordType

jdurante
24 Apr 2009, 7:34 AM
Yes, it is the record constructor for the columnStore, this is columnMeta:



var columnMeta = Ext.data.Record.create([
{name: 'Header'},
{name: 'DataIndex'},
{name: 'IsHidden'},
{name: 'Renderer'},
{name: 'Width'},
{name: 'OrderIndex'},
{name: 'ColumnID'}
]);
The record constructor that I'm trying to make for the Data Store is:



var fnames = Ext.data.Record.create([
{name: 'Order #'},
{name: 'Priority'},
{name: 'Web/Order Date'},
{name: 'Account #'},
{name: 'Company'},
{name: 'Order Descript'},
{name: 'Price'},
{name: 'Contact'},
{name: 'Initials'},
{name: 'Is Saved'},
{name: 'BFP'}
]);

I'm an Ext newbie, so I'm still confused. I tried to use the recordType but it's still not working.

I tried:



reader: new Ext.data.JsonReader({
root: 'orders',
recordType: fieldArray
}),
and



reader: new Ext.data.JsonReader({
root: 'orders',
recordType: Ext.data.Record.create(fieldArray)
}),
I can see in the DOM in Firebug, that recordType is undefined, so I know I'm doing something wrong, I'm just not sure what it is.

I also tried to print my fieldArray to the console before the orderStore is created and it prints an empty array. So, I'm wondering if the columnStore has not finished creating the fieldArray before I am setting up orderStore?


Thanks for your help.

Animal
24 Apr 2009, 8:46 AM
Too much there. It's knackered. You need to really read those API docs and the examples.

jdurante
24 Apr 2009, 8:54 AM
Wow, that bad, huh?

I thought I was really starting to understand Ext too!

Well, thanks anyways for the help.

Jon