View Full Version : combo default selection

13 Sep 2012, 8:22 AM

how to make the last value of the store for default selection value.
I mean the last value of the store should come selected on render of the combo.

Tim Toady
13 Sep 2012, 11:12 AM
If you know the value just put it in the config as the value property. If you do not then you will need to listen to the stores load event and then call its last (http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.Store-method-last)function or do something similar to get the last record/records value. Then use something like select (http://docs.sencha.com/ext-js/4-1/#!/api/Ext.form.field.ComboBox-method-select) or setValue (http://docs.sencha.com/ext-js/4-1/#!/api/Ext.form.field.ComboBox-method-setValue) on the combo.

14 Sep 2012, 8:36 AM
Hi i tried some thing like that

xtype : 'combo',
fieldLabel : 'From',
labelWidth : 35,
width : 200,
itemId : 'timeframefrom',
displayField : 'timeFrameFromToOption',
valueField : 'timeFrameFromToOption',
listeners : {
render : function(combo,record){
var store = combo.getStore();
var index = store.find('timeFrameFromToOption', combo.getValue());
alert(index); //getting -1
if(index != -1)//the record has been found
var record = store.getAt(index);
//you can then do what you like with the record.
// alert(refStore.getAt(0));
//var store = Ext.data.StoreManager.lookup('fromToOptions');
// alert(this.getStore.getAt(0).get('timeFrameFromToOption'));
// var rec = Ext.StoreMgr.lookup("MyStore").getAt(index);
// alert(combo.getStore().getAt(0));
// this.select(this.getStore().getAt(0));// getting un defined
in side listener i tried but unable to get the any value from the store.
Actually the store of the combobox is dependant on another combo box.

On selection and render of first combo the second combo is getting populated through Ajax request. So how to set a default value from the store for the second combo.

Help me out plz.

Tim Toady
14 Sep 2012, 10:38 AM
You are calling store.load and then immediately trying to access its data. Loading a store is going to do an ajax request, which is asynchronous. This means when you call store.load it doesn't wait for the data to return. It just continues. If you want to get its data after it loads you need to use a callback. This can either be done by using the callback (http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.Store-method-load)property as seen in the api example or by adding a listener to the store using something like store.on( 'load', yourFunc ). The way you have code laid out currently I would go with the former option.