PDA

View Full Version : How to set selected extjs combo value to another combobox



soft_Dev
26 Sep 2013, 10:40 PM
I have two Extjs Comboboxes


var cb1 = new Ext.form.ComboBox({
fieldLabel: 'Combobox1',
width: 100,
listeners:{
scope: this,
'select': this.reset
}
});

var cb2 = new Ext.form.ComboBox({
fieldLabel: 'Combobox2',
width: 100,
baseParams: { loadInitial: true, cb1Val: this.cb1.getValue() } // Here I want to get selected value of cb1 listeners:{
scope: this,
'select': this.reset
}
});


I am using this.cb1.getValue() to get selected value of cb1 but I am getting blank value here.
Can anyone tell me what I am doing wrong here.

Farish
26 Sep 2013, 11:18 PM
have you attached a store to the combobox? have you set up the valueField config? i dont see this in the code you have posted.

soft_Dev
26 Sep 2013, 11:26 PM
Yes I have set the store so combobox is having two values 'Yes' and 'No'

I have not set valuefield though. Can you please let me know how can I do it.

Farish
26 Sep 2013, 11:28 PM
take a look at the example code in the API docs:
http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.form.field.ComboBox

you have to define one field as the valueField for the combobox and when you use getValue(), the value corresponding to that field in the store will be returned.

soft_Dev
27 Sep 2013, 12:37 AM
I followed the link but still not able to get the selected balue of cb1. :-|

Farish
27 Sep 2013, 12:46 AM
look at this example:


// The data store containing the list of states
var states = Ext.create('Ext.data.Store', {
fields: ['abbr', 'name'],
data : [
{"abbr":"AL", "name":"Alabama"},
{"abbr":"AK", "name":"Alaska"},
{"abbr":"AZ", "name":"Arizona"}
//...
]
});

// Create the combo box, attached to the states data store
var cb1 = Ext.create('Ext.form.ComboBox', {
fieldLabel: 'Choose State',
store: states,
queryMode: 'local',
displayField: 'name',
valueField: 'abbr',
renderTo: Ext.getBody(),
listeners:
{
select: function(combo, records)
{
var val = combo.getValue();
alert(val);
}
}
});

you will see that whenever you select a value in the combobox, it will alert it. One reason why it might not be working in your case is that when you call getValue(), may be no value is set on your first combobox. make sure you call it after a value has been selected by user or you must explicitly set the value in the config of cb1.

LarssaAndin
27 Sep 2013, 3:46 AM
I agree with Farish that the main problem is that when cb2 is created, cb1 hasn't been intialized yet so no value is available to be read. It would be easier to help you if we understand what you want to accomplish, the example code you give is too limited to really understand what you want to do.

If you want to update/create cb2 when selecting something in cb1 then you have to do that in the select and/or blur event handler for cb1 (why do you reset the combo on select?). If you really want to base content of cb2 on what cb1 has as start value then you should just use the input data of cb1 as parameter in cb2 and not try and read the value from the combobox.