Pages

Wednesday, May 4, 2011

ExtJS -=- ComboBox TypeAHead useful hack

Hi

Here is a useful hack that disable the auto selection of the first record while searching data in a combo box with the Type Ahead functionnality.

If there is only one record, the combo will automatically selects the first record. You just need to add this override to your code.




/**
 * When we are looking for an item in a combo box by typing data in the combo
 * box, the combo usually used to take the first record and replace the actual
 * typed data. This override disable this functionnality so we can enter data
 * and use the filters.
 *
 * If there is only one result, the combo automatically selects the first record.
 *
 */
Ext.override(Ext.form.ComboBox, {
    onTypeAhead : function(){
        if(this.store.getCount() === 1){
            var r = this.store.getAt(0);
            var newValue = r.data[this.displayField];
            var len = newValue.length;
            var selStart = this.getRawValue().length;
            if(selStart != len){
                this.setRawValue(newValue);
                this.selectText(selStart, newValue.length);
            }
        }
    }
});

No comments:

Post a Comment