Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set combobox's value from extjs controller

I am working in extjs4. I have combobox with code as-

{
margin:'7 6 5 5',
xtype:'combobox',
fieldLabel:'Language',
name:'language',
id:'combo',
width:190,
allowBlank:false,
emptyText: '--Select language--',
labelWidth: 60,
store: new Ext.data.ArrayStore({
    fields: ['id','value'],
    data: [
        ['1','Marathi'],
        ['2','English']
    ]
}),
queryMode: 'local',
valueField:'id',
displayField:'value',
editable:false
},

Throuhout my functioning I want to set combobox's value defaultly to user's selected choice. So how to set combobox's value from controller

like image 285
user1722857 Avatar asked Jul 12 '13 09:07

user1722857


1 Answers

To select default value you can use event listener. After combobox has been rendered you can set value you need using setValue() method from Ext.form.field.Field and if you need to select combobox value on demand you can get it using Ext.getCmp('combo') and then use setValue() or even better set itemId instead of id and use componentQuery to fetch combobox and set value:

Ext.ComponentQuery.query('#combo')[0].setValue('2');

setValue( value ) : Ext.form.field.Field Sets the specified value(s) into the field. For each value, if a record is found in the store that matches based on the valueField, then that record's displayField will be displayed in the field. If no match is found, and the valueNotFoundText config option is defined, then that will be displayed as the default field text. Otherwise a blank value will be shown, although the value will still be set.

listeners:{
    scope: this,
    afterRender: function(me){
        me.setValue('1');   
    }
}

Here is an example in fiddle

like image 171
Davor Zubak Avatar answered Sep 22 '22 17:09

Davor Zubak