Search code examples
extjs4multi-selectitemselector

ExtJS itemselector with search/filter option


I am looking for a ExtJS itemselector with search/filter option. Is there a existing plugin for this or any ideas to implement it? Since itemselector internally uses Multiselect, do I need to implement my own version on Multiselect?


Solution

  • The multiselect extension has an option for configuring the tbar (Top bar) property. I used the following code to configure the "toField" of itemselector:

     tbar : {
                    xtype: 'toolbar',
                    flex: 1,
                    dock: 'top',
                    items: [
                        'Filter:',
                        {
                            xtype: 'textfield', 
                            fieldStyle: 'text-align: left;',                            
                            enableKeyEvents: true,
                            listeners: {
                                scope: this,
                                change : function(field, newValue, oldValue, options) {                                 
                                    var toStore = this.toField.boundList.getStore();                                    
                                    toStore.clearFilter();
                                    if (String(newValue).trim() != "")
                                    {
                                        toStore.filterBy(function(rec, id){
                                            return this.filterFunc(rec, newValue);  
                                        }, this);
                                    }
                                }
                            }
                        }
                    ]
                }
    

    And my filterFunc is :

    filterFunc: function(rec, filter)
    {        
        var value = rec.get(this.displayField);
    
        if (this.filterIgnoreCase) value = value.toLocaleUpperCase();
        if (this.filterIgnoreCase) filter = filter.toLocaleUpperCase();
    
        if (Ext.isEmpty(filter)) return true;
    
        if (this.filterAnyMatch && this.filterWordStart)
        {
            var re_opts = this.filterIgnoreCase ? 'i' : '';
            var re = new RegExp('(^|[\\s\\.!?;"\'\\(\\)\\[\\]\\{\\}])'+Ext.escapeRe(filter), re_opts);
            return re.test(value);
        }
        else if (this.filterAnyMatch)
        {
            var re_opts = this.filterIgnoreCase ? 'i' : '';
            var re = new RegExp(Ext.escapeRe(filter), re_opts);
            return re.test(value);
        }
        else
        {
            var re_opts = this.filterIgnoreCase ? 'i' : '';
            var re = new RegExp('^\s*'+Ext.escapeRe(filter), re_opts);
            return re.test(value);
        }
    }