PDA

View Full Version : [SOLVED] Json & Checkbox



avo
18 Nov 2009, 8:14 AM
Hey there,

i've got a little problem with the following code. I've got some dynamicly generated checkboxes, which should be checked, if they're set in the json record.



....

//Dynamic Boxes
var boxes = [];
var count = store.getCount();
while(count--){
boxes[count] = {boxLabel: records[count].data.name, inputValue: records[count].id, name: 'many[categorys][category][' + count + '][category_id]', checked: false};
}
this.boxes = boxes;

....


// Panel
this.form = new Ext.FormPanel({
disabled: true,
fileUpload: true,
columnWidth: 0.5,
frame: true,
reader: new Ext.data.JsonReader({
root: 'records',
fields:[
{name: 'id'},
{name: 'name'},
{name: 'articlenumber'},
{name: 'startprice'},
{name: 'starttime', type: 'date', dateFormat: 'c'},
{name: 'endtime', type: 'date', dateFormat: 'c'},
{name: 'picturepath'},
{name: 'description'}
]
}),
buttons: [{
text: '<?= ucfirst($this->translate('submit'))?>',
handler: function(){
this.form.getForm().submit({
url: '<?= $this->url(array('controller' => 'article', 'action' => 'edit', 'format' => 'json')) ?>',
method: 'POST',
waitTitle: '<?= ucfirst($this->translate('global_save_title'))?>',
waitMsg: '<?= ucfirst($this->translate('global_save_message'))?>',
scope: this,
success: function(form, action){
if(this.form.currentRecord != null){
this.form.getForm().updateRecord(this.form.currentRecord);
}
this.form.findByType('image')[0].setSrc(Ext.ux.Image.src);
this.grid.getSelectionModel().unlock();
this.form.disable();
this.grid.getTopToolbar().get(1).setDisabled(false);
this.form.getForm().reset();
this.store.commitChanges();
if(this.form.currentRecord == null){
this.store.reload();
}

delete this.form.currentRecord;
},
failure: function(){
alert('did not work!');
}
});
}, scope: this
},{

......

//Checkboxgroup impl.
items: [{ .... },{
xtype: 'checkboxgroup',
fieldLabel: '<?= ucfirst($this->translate('category'))?>',
columns: 3,
border: true,
items: this.boxes
}]

........ My Json-response looks like the following:


{"total":null,"records":[{"id":15,"startprice":"100.00","starttime":"2009-11-16 16:55:00","endtime":"2009-11-20 16:55:00","articlenumber":"pic4","name":"1te Hochzeit zu Kana asdf","picturepath":"1.jpg","description":"asdf","many":{"categorys":{"category":[{"category_id":20},{"category_id":23},{"category_id":24}]}}}],"success":true}
Any ideas? :)


Best Regards, avo ~o)



EDIT:

solved my problem, i had to add the following to my forms readers fields:




{name: 'categories', mapping: 'many.categorys.category', convert:
function(v, rec){
if (v instanceof Array) {
var categories={};
for(var j = v.length-1; j >= 0; j--){
categories['many[categorys][category]['
+ v[j].category_id + '][category_id]']=true;
}
return categories;
}
}}


greetings ;)