PDA

View Full Version : Set value on a searching combo box - SOLVED



markporter
27 Mar 2011, 8:10 AM
I often use type-ahead or searching combo boxes in my forms. This is great for the initial create, but when I call setValues on the form from a callback, I end up with my ID displayed instead of the label. This is because the store for the combo is empty when the value is set. Often there are thousands of records behind the store, so pre-loading the entire result set is impracticable. Here is the solution I found to this problem:

Add this hack somewhere before you load any combos (doesn't have to be after document ready)



Ext.form.ComboBox.prototype.nativeSetValue = Ext.form.ComboBox.prototype.setValue;
Ext.form.ComboBox.prototype.setValue=function(v){
var combo = this;
if(this.valueField){
var r = this.findRecord(this.valueField, v);
if (!r) {
var data = {}
data[this.valueField] = v
this.store.load({
params:data,
callback:function(){
combo.nativeSetValue(v)
}
})

} else return combo.nativeSetValue(v);
} else combo.nativeSetValue(v);
}


This basically will check if your value is in your store, and if it is not, do a callback with valueField = value and then try setting again. You just need to make sure your serverside handler looks for "query" for searches and the key field for loads