Alexander
Alexander

Reputation: 20244

Load object into a form

I know how to add a store record into a form.

But now I have an object which is not part of a store.

I loaded it using the helper function

var fields = someFormPanel.getForm().getFields();
Ext.each(fields.items,function(field, i) {
    if('name' in field && field.name in obj) field.setValue(obj[field.name]);
});

but afterwards I asked myself whether there already is a predefined function?

Upvotes: 1

Views: 128

Answers (1)

Akatum
Akatum

Reputation: 4016

You can use setValues method from Ext.form.Basic object. If you use for your form Ext.form.Panel class you can get its underlying Ext.form.Basic with getForm() method.

var data = {
    firstName: 'Homer',
    lastName: 'Simpson'
}

var form = Ext.create('Ext.form.Panel', {
    title: 'Simple Form',
    bodyPadding: 5,
    width: 350,
    defaultType: 'textfield',
    items: [{
        fieldLabel: 'First Name',
        name: 'firstName',
        allowBlank: false
    },{
        fieldLabel: 'Last Name',
        name: 'lastName',
        allowBlank: false
    }],
    renderTo: Ext.getBody()
});

form.getForm().setValues(data);  

Fiddle with example: https://fiddle.sencha.com/#fiddle/2op

Upvotes: 1

Related Questions