PDA

View Full Version : JsonReader and multidimensional data



perkinsit
13 Oct 2009, 7:55 AM
First let me say thanks for a great product and all of the help I see on these forums. As a noobie, its nice to have so much support!

Ok, so here is my issue. I have a form that I am trying to populate with data from a database record passed back to the JsonReader. Using Firebug, I can see that the request is sent and the correct Json string is passed back to the reader. Here is an example of the Json I am receiving from the remote server:


({"success":true,"cons_record":{"cons_main":[{"cons_id":"1","client_id":"1","cons_fname":"Matthew","cons_lname":"Perkins","cons_ss":"342-99-4453","cons_dob":"0000-00-00","cons_gender":"Male","cons_medicaid":null,"cons_mod":"2009-08-19 14:37:51","cons_crea":"0000-00-00 00:00:00"}]}})And here is my form:

var ConsGrid = new Ext.grid.GridPanel({
layout:'anchor',
id:'consListGrid',
autoHeight: 'auto',
frame:true,
ds: consList_store,
cm: new Ext.grid.ColumnModel([
{
header: 'Last Name',
dataIndex: 'cons_lname',
width: 125,
sortable: true
},{
header: 'First Name',
dataIndex: 'cons_fname',
width: 125,
sortable: true
},{
header: '#',
dataIndex: 'cons_id',
width: 30,
sortable: true,
hidden: true
}
]),//colModel,
tbar: [{
text: 'Add Consumer',
iconCls: 'add16',
handler : addConsumerPrompt
},
'->',
{
text: 'Reload Consumers',
handler : reload_cons_grid
}],
sm: new Ext.grid.RowSelectionModel({
singleSelect: true,
listeners: {
rowselect: function(sm, row, rec) {
//clear the consumers form
cons_edit_form.getForm().reset();

//load the consumers full record from the database
cons_edit_form.getForm().load({
url: '/consumer/consumer/getConsumer',
method:'POST',
params:{cons_id:rec.data.cons_id}
});
}
}
}),
listeners:{
render: function(){
this.store.load();
}
}

});

var cons_edit_form = new Ext.FormPanel({
id: 'list-panel',
frame: true,
method:'POST',
labelAlign: 'left',
title: 'Consumers',
bodyStyle:'padding:5px',
autoScroll:true,
width: '100%',
waitMsgTarget:true,
reader: new Ext.data.JsonReader({
root: 'cons_record',
idProperty: 'cons_id',
fields: [
//consumer_main
{name: 'cons_id', type: 'string', mapping: 'cons_main.cons_id'},
{name: 'cons_fname', type: 'string', mapping: 'cons_main.cons_fname'},
{name: 'cons_lname', type: 'string', mapping: 'cons_main.cons_lname'}
]
}),

layout: 'column',
items:[{
width: 252,
layout: 'fit',
items: [
ConsGrid
]
},{
columnWidth: 1,
layout: 'form',
xtype: 'fieldset',
labelWidth: 110,
title:'Consumer details',
defaults: {width: 140, border:false}, // Default config options for child items
defaultType: 'textfield',
autoHeight: true,
bodyStyle: Ext.isIE ? 'padding:0 0 5px 15px;' : 'padding:10px 15px;',
border: true,

style: {
"margin-left": "10px", // when you add custom margin in IE 6...
"margin-right": Ext.isIE6 ? (Ext.isStrict ? "-10px" : "-13px") : "0" // you have to adjust for it somewhere else
},
items: [{
fieldLabel: 'Last Name',
name: 'cons_lname'
},{
fieldLabel: 'First Name',
name: 'cons_fname'
},{
name: 'cons_id',
value: '1',
hidden: true
}],
buttons: [{
text: 'Submit',
disabled:true, //starts the blank form with a disabled button.
id:'form_edit_submit',
listeners: {
click: function(){
cons_edit_form.getForm().submit({
url:'/consumer/consumer/editConsumer',
cmd:'submit',
waitMsg:'Saving consumer information...',
success: function(){
Ext.Msg.alert('Status','Consumer Updated');
consList_store.reload();
cons_edit_form.getForm().reset();
},
failure: function(form, action){
Ext.Msg.alert('Error',
'Type: '+action.response.statusText+
'Status: '+action.response.status);

}
})
}
}
},{
text: 'Reset',
listeners: {
click: function(){
cons_edit_form.getForm().reset();
}
}
}]
}]

});I am having trouble getting the data to populate the form. Basically, this is a gridPanel that when clicked, that persons record is retrieved from the database and used to populate the form (cons_edit_form) so it can be edited and submitted back to the server for saving. I was doing OK until I made the Json string from the server multidimensional. You can see how I mapped the fields in the reader. This is really a proof of concept, as I hope to be able to load about 50 fields of data in the multidimensional Json string from the server. Any help would be greatly appreciated.

I also get no errors from firebug.

Also, is there any to echo or display an object or array without knowing the keys or the depth? I use PHP and print_r() often lets me verify what a variable or array contains, etc. Hunted for this for quite a while to no avail.

Thanks for any help you can give. I really appreciate it. Sorry for the long code but wasn't sure how much background would be needed.

29 Oct 2009, 5:21 AM
set the store's root to: "cons_record.cons_main"