PDA

View Full Version : Combos default value



elnan14
12 Aug 2010, 2:55 PM
This is driving me crazy... I have a simple form with a few combos loaded from a sql server database via stores. I need a default value in each of the combos, the first element in the list.

I tried to select the first element after the store loads but the problem is that the store loads only when I expand the list of the combo for the first time and I need the default values right when the page loads. I tried to load the combo at the startup with no luck.

How can I achieve this?

Thank you very much and sorry, I'm new in the ExtJS world.

ashaihullin
12 Aug 2010, 9:10 PM
so, what about this:


this.myCombo.on('render', function() { this.myStore.load(); }, this);
this.myStore.on('load', function() { this.myCombo.setValue( 0 ); }, this);

elnan14
12 Aug 2010, 10:46 PM
Thank you very much ashaihullin for pointing me in the right direction :)

At the end, I came up with this code:



SuperComboBox = Ext.extend(Ext.form.ComboBox, {
constructor: function(cfg) {
cfg = cfg || {};
var self = this;
ComboBoxX.superclass.constructor.call(this, cfg);

this.selectFirst = cfg.selectFirst || false;

if(this.selectFirst) {
this.on('render', function(c) {
var callback = function() {
var record = c.store.getAt(0);
if(record) {
c.setValue(record.get(c.valueField))
self.fireEvent('select', c, record, 0);
}
};

if(c.store.constructor.toString().indexOf('Ext.data.ArrayStore.superclass.constructor') >= 0)
callback();
else
c.store.load({callback: callback});
}, this);
}
}
});

var my_combo = new SuperComboBox({
store: my_store
valueField: 'value',
displayField: 'label',
id: 'my_combo',
triggerAction: 'all',
forceSelection: true,
typeAhead: true,
selectFirst: true
});

It works!!

But I'm still having the problem that the store loads twice, the first time when the combo renders and the second time when the user expands the combo :s

Any thoughts on how to stop this behavior?

Thanks!

ashaihullin
12 Aug 2010, 11:06 PM
add property "mode" to your combo:



var my_combo = new SuperComboBox({
store: my_store
valueField: 'value',
displayField: 'label',
id: 'my_combo',
triggerAction: 'all',
forceSelection: true,
typeAhead: true,
selectFirst: true,
mode: 'local'
});

elnan14
12 Aug 2010, 11:36 PM
It finally works!!

Thank you very much for your help! :D

ashaihullin
12 Aug 2010, 11:52 PM
youre welcome ))