Search code examples
extjscomboboxmulti-select

Filtering in ExtJS combobox with multiSelect set as true


Is there a way to enable filtering in combobox with multiSelect enabled ? I know multiSelect has been deprecated and Sencha recommends using tagfield, but my requirement is for combobox. For the first time the filtering works fine, but next time the selected values are not retained.


Solution

  • From what I understand, combobox is not retaining the previous selection on selecting the next filtered option.

    For this, I looked into the valueCollection array of Combobox, which stores the selected records. In getValue method, selection is explicitly getting removed. Seems like they haven't thought about multiselect in this case.

    We can add a simple check in the override to have it fixed:

    Ext.define('Overrides.form.Field.Combobox', {
        override: 'Ext.form.field.ComboBox',
        getValue: function() {
            var me = this,
                store = me.getStore(),
                picker = me.picker,
                rawValue = me.getRawValue(),
                value = me.value;
            
            if (!store.isEmptyStore && me.getDisplayValue() !== rawValue) {
                me.displayTplData = undefined;
                // BEGIN OVERRIDE
                if (picker && !me.multiSelect) {
                    me.valueCollection.suspendEvents();
                    picker.getSelectionModel().deselectAll();
                    me.valueCollection.resumeEvents();
                    me.lastSelection = null;
                }
                // END OVERRIDE
    
                if (store.isLoaded() && (me.multiSelect || me.forceSelection)) {
                    value = me.value = undefined;
                } else {
                    value = me.value = rawValue;
                }
            }
            
            me.value = value == null ? null : value;
            return me.value;
        }
    });
    

    Here is the working fiddle: https://fiddle.sencha.com/#view/editor&fiddle/3nro