在ExtJS中设置表单字段值

时间:2010-03-14 18:58:50

标签: javascript extjs

我正在使用ExtJS创建一个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'
    }]
})

如何通过名称访问表单字段以手动设置其值?感谢

3 个答案:

答案 0 :(得分:24)

这很容易:

  • 获取表单面板(顺便提一下Ext.form.FormPanel而不只是Ext.FormPanel):

    var formPanel = new Ext.form.FormPanel({...});
    
  • 获取基础Ext.form.BasicForm

    var form = formPanel.getForm();
    
  • 然后,您可以使用findField(name)按名称检索表单字段:

    var cellField = form.findField('cell_phone');
    

答案 1 :(得分:20)

您也可以使用setValues()方法批量设置它们。

例如:

Ext.getCmp('formname').getForm().setValues({
fielda: 'value1', 
fieldb: 'value2' 
})

答案 2 :(得分:3)

尼斯!为我工作:D

但您可以设置默认值:

  

...
  项目:[{
           的xtype: '文本框',
           fieldLabel:'名字',
           名称:'first_name',
           id:'first_name',
           值:'somevalue'
           主播:'95%'
         },
  ...