Extjs combobox: hide selected value from dropdown list

后端 未结 5 1449
青春惊慌失措
青春惊慌失措 2021-02-15 14:57

I\'m using ExtJS 4 and looking for a way I can hide currently selected value from combo\'s dropdown list?

So instead of this (\"Alaska\" currently selected in combobox):

5条回答
  •  庸人自扰
    2021-02-15 15:09

    I ended up using a modified version of @sra's solution:

    Ext.define('My.ComboBox',  {
        extend: 'Ext.form.field.ComboBox',
    
        /**
         * @cfg {Boolean} hideActive=true
         * When true, hides the currently selected value from the dropdown list
         */
        hideActive: true,
    
        /**
        * @private {Ext.data.Model[]} selectedRecords
        * A Array of selected records, used when hideActive is true
        */
    
        initComponent: function() {
            this.selectedRecords = [];
    
            this.callParent();
        },
    
    
        setValue: function(value, doSelect) {
            var store = this.store;
    
            if(this.hideActive) {
                store.suspendEvents(false);
                // write the current selected back to the store (you need to suspend autoSync if active)
                // do this BEFORE callParent so the currently selected record would be found in the store
                store.add(this.selectedRecords);
            }
    
            this.callParent(arguments);
    
            if(this.hideActive) {
                // set the selected as new recordlist
                this.selectedRecords = this.valueModels;
                // remove the selected from the store
                store.remove(this.valueModels);
                store.resumeEvents();
                store.fireEvent('refresh', store);
            }
    
            return this;
        }
    
    });
    

    The 'hiding' logic is the same, only I perform it in the setValue method to make sure that it also works when programmatically setting combo's value, including the case where the combobox is initialized with a value.

    UPD Also, looks like store.add(this.selectedRecords); has to be called before this.callParent(arguments);, otherwise combobox will act weird if we attempt to set the same value twice (it simply wouldn't find the active record in the store cause we removed it, so it'll reset to blank). I suspend store's events to prevent some quirks caused by combobox trying to synchronize with its dropdown list in the middle of my manipulations with selected record(s) and manually trigger store's 'refresh' event when I'm done so the list gets eventually updated. This may have performance impact but so far I don't know a better solution.

提交回复
热议问题