PDA

View Full Version : Preselection of Combobox Entry?



consolex
28 Sep 2009, 9:49 AM
Hi,

i have attached a listener to my Combobox. What i am desperately trying is to preselect a value in the combobox when showing it. The preselection should also trigger my "select"-listener.

How can i achieve to e.g. select the field with value=3 and get the "select"-listener triggered?




var combo = new Ext.form.ComboBox({
id: 'store_combo',
store: new Ext.data.SimpleStore({
fields: ['id', 'name'],
.......
.......
listeners:{select:{fn:function(field, value) {
selected_name = value.data.name;
selected_id = value.data.id;
selected = true;
Ext.Ajax.request({
url: '<?php echo $this->getUrl("*/*/dosomething")?>'+'?selectID='+selected_id,
Thx for helping :((

zhegwood
28 Sep 2009, 12:43 PM
You can specify a value through the "value" config option, but calling setValue() does not fire the select event.

So, you can specify the value and then in the render listener, make your ajax call if there's a value.



var combo = new Ext.form.ComboBox({
id: 'store_combo',
store: new Ext.data.SimpleStore({
fields: ['id', 'name'],
.......
.......}),
value: someval,
listeners:{
render: {
fn:function(field) {
selected_name = someval;
selected_id = someval;
selected = true;
if (selected_id !== "") {
Ext.Ajax.request({
url: '<?php echo $this->getUrl("*/*/dosomething")?>'+'?selectID='+selected_id,
...
});
}
}
}
}
});

dbassett74
28 Sep 2009, 1:01 PM
Just in case you're interested, here is an override that I use in order to optionally specify that calling setValue triggers the "select" event. However, it does have a shortcoming in that it doesn't fire beforeselect, but unless you need to do some processing/validation before the value is actually set, it won't matter. It works for 99% of the situations that I have. I'm hoping someone can help with that...



Ext.override(Ext.form.ComboBox, {
setValue: function(v, fireSelect) {
var text = v;
if (this.valueField) {
var r = this.findRecord(this.valueField, v);
if (r) {
text = r.data[this.displayField];
var index = this.store.indexOf(r);
this.selectedIndex = index
if (fireSelect) {
this.fireEvent('select', this, r, index);
}
} else if (Ext.isDefined(this.valueNotFoundText)) {
text = this.valueNotFoundText;
}
}
this.lastSelectionText = text;
if (this.hiddenField) {
this.hiddenField.value = v;
}
Ext.form.ComboBox.superclass.setValue.call(this, text);
this.value = v;
return this;
}
});