PDA

View Full Version : How to get the 'id' of the selected option in a combobox ?



sunz2009
21 Feb 2009, 3:09 AM
I have created a form with a combobox in it.
the code:


var form = new Ext.FormPanel({
labelAlign:'left',
labelWidth:141,
frame:true,
renderTo:'form',
title:'choose sex',
items: [{
xtype:'combo',
editable:false,
displayField:'sex',
mode:'local',
store:new Ext.data.SimpleStore({
fields:['id','sex'],
data:[['0','male'],['1','female']]
}),
fieldLabel:'sex',
name:'sex',
id:'sex',
triggerAction:'all'
}]
});
Now I can only get the value in the combobox('male' or 'female') when I selected an option in it by using:

alert(Ext.get('sex').getValue());
But If I want to get the id of the selected option('0' or '1'), how should I do ?
I have used:

alert(Ext.get('sex').getValue().id);
But it shows 'undefined' .
If anyone knows the way, please tell me.
Thank you !

faisal abbas
21 Feb 2009, 4:34 AM
//add a listener propety in combo Box
listeners: {
select: function(combo, record, index){
id=record.data['id'];//**** Id variable could be Global
Ext.MessageBox.alert('Value',id);

}
}

sunz2009
21 Feb 2009, 5:17 PM
It works !
You just earned your weight in gold.
Thank you !

mjlecomte
21 Feb 2009, 6:14 PM
you did not specify valueField (http://extjs.com/deploy/dev/docs/?class=Ext.form.ComboBox&member=valueField)

If you want to post that to the server, also utilize the hiddenName config.