Ext-JS setValue method for ComboBox Control

Trying to set the selected value of a ComboBox control in Ext-JS 2.2, I found out that it was trying to set the selection before it was actually populated. This in-turn appended the value into the ComboBox. Ext's FAQ lists a patch to work around this, but it took some searching to find this solution. You will need to overwrite the setValue method that will listen for the store to be populated.

Ext.override(Ext.form.ComboBox, {
setValue : function(v){
//begin patch
// Store not loaded yet? Set value when it *is* loaded.
// Defer the setValue call until after the next load.
if (this.store.getCount() == 0) {
this.store.on('load',
this.setValue.createDelegate(this, [v]), null, {single: true});
return;
}
//end patch
var text = v;
if(this.valueField){
var r = this.findRecord(this.valueField, v);
if(r){
text = r.data[this.displayField];
}else if(this.valueNotFoundText !== undefined){
text = this.valueNotFoundText;
}
}
this.lastSelectionText = text;
if(this.hiddenField){
this.hiddenField.value = v;
}
Ext.form.ComboBox.superclass.setValue.call(this, text);
this.value = v;
}
});

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
byrus's Gravatar Thanks Chris. This code help me a lot
# Posted By byrus | 6/25/09 6:40 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.1. Contact Blog Owner