Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set form field values in ExtJS

I'm using ExtJS to create a formPanel:

new Ext.FormPanel({
    labelAlign: 'top',
    title: 'Loading Contact...',
    bodyStyle:'padding:5px',
    width: 600,
    autoScroll: true,
    closable: true,
    items: [{
        layout:'column',
        border:false,
        items:[{
            columnWidth:.5,
            layout: 'form',
            border:false,
            items: [{
                xtype:'textfield',
                fieldLabel: 'First Name',
                name: 'first_name',
                id: 'first_name',
                anchor:'95%'
            }, {
                xtype:'datefield',
                fieldLabel: 'Birthdate',
                name: 'birthdate',
                width: 150,
            }]
        },{
            columnWidth:.5,
            layout: 'form',
            border:false,
            items: [{
                xtype:'textfield',
                fieldLabel: 'Last Name',
                name: 'last_name',
                anchor:'95%'
            },{
                xtype:'textfield',
                fieldLabel: 'Email',
                name: 'email',
                vtype:'email',
                anchor:'95%'
            }]
        }]
    },{
        xtype:'tabpanel',
        plain:true,
        activeTab: 0,
        height:300,
        /*
         * By turning off deferred rendering we are guaranteeing that the
         * form fields within tabs that are not activated will still be
         * rendered. This is often important when creating multi-tabbed
         * forms.
         */
        deferredRender: false,
        defaults:{bodyStyle:'padding:10px'},
        items:[{
            title:'Address',
            layout:'form',
            defaults: {width: 230},
            defaultType: 'textfield',

            items: [{
                fieldLabel: 'Line1',
                name: 'line1',
                allowBlank:false,
            },{
                fieldLabel: 'Line2',
                name: 'line2',
            },{
                fieldLabel: 'City',
                name: 'city',
                allowBlank: false,
            },{
                xtype:"combo",
                fieldLabel:"State",
                name:"state",
                hiddenName:"combovalue"
              }, {
                fieldLabel: 'Zipcode',
                name: 'zipcode',
                allowBlank: false,
            }]
        },{
            title:'Phone Numbers',
            layout:'form',
            defaults: {width: 230},
            defaultType: 'textfield',

            items: [{
                fieldLabel: 'Home',
                name: 'home_phone',
            },{
                fieldLabel: 'Cell',
                name: 'cell_phone'
            },{
                fieldLabel: 'Emergency',
                name: 'emergency_phone'
            }]
        },{
            cls:'x-plain',
            title:'Notes',
            layout:'fit',
            items: {
                xtype:'htmleditor',
                name:'notes',
                fieldLabel:'Notes'
            }
        }]
    }],

    buttons: [{
        text: 'Save'
    },{
        text: 'Cancel'
    }]
})

How do I access the form fields by the name to set their value manually? Thanks

like image 803
jeremib Avatar asked Mar 14 '10 18:03

jeremib


People also ask

How to submit form in Ext JS?

In Ext JS, the Form panel handles the submission of form data for you using its built-in submit method. The form collects up all of the values in the form and posts the form data to your remote server. The controller logic to handle the submission is pretty straightforward: Get the form >> form. submit() .

What is tooltip in Ext JS?

ToolTip is a Ext. tip. Tip implementation that handles the common case of displaying a tooltip when hovering over a certain element or elements on the page. It allows fine-grained control over the tooltip's alignment relative to the target element or mouse, and the timing of when it is automatically shown and hidden.

What is Sencha Ext JS?

A lightweight IDE optimized for building UI web tests with Selenium or Protractor.

What is the use of formBind true when applied on a button?

Any component within the FormPanel can be configured with formBind: true . This will cause that component to be automatically disabled when the form is invalid, and enabled when it is valid.


1 Answers

It's quite easy:

  • get hands on the form-panel (by the way it's Ext.form.FormPanel and not just Ext.FormPanel):

    var formPanel = new Ext.form.FormPanel({...});
    
  • get the underlying Ext.form.BasicForm

    var form = formPanel.getForm();
    
  • you then can use findField(name) to retrieve your form fields by their names:

    var cellField = form.findField('cell_phone');
    
like image 92
Stefan Gehrig Avatar answered Oct 05 '22 11:10

Stefan Gehrig