5

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

2
  • Aminesrine's answer is technically correct, but best practice would dictate that you not use an "id". It would be better to assign an itemId, or retrieve the instance of the combobox via a ComponentQuery. Commented Jul 12, 2013 at 10:39
  • Ya sir i am also trying same. i have written code as- id=Balaee.controller.qb.QbqnsController.ID; var getlanguageId =Ext.getCmp('combo'); if(id==1) { getlanguageId.setValue('1','Marathi'); } else { getlanguageId.setValue('2','English'); } But its setting value of combobox as[object HTMLDivElement] So what i need to modify Commented Jul 12, 2013 at 10:48

2 Answers 2

9

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

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

Ext.getCmp('combo').setValue('1' or '2');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.