Ext JS:过滤组合框的正确技术

Ext JS: Proper technique to filter a combobox?

本文关键字:技术 组合 JS 过滤 Ext      更新时间:2023-09-26

当我通过在底层存储中添加过滤器来过滤组合框时,有时过滤器有效(条目被删除),有时没有效果。我已经调试了filterBy函数;当我希望过滤/显示项目时,它正在被调用并返回true/false。

我在ExtJS论坛上读到,"组合框使用过滤(即使使用triggerAction:'all'),所以你自己的触发器被组合框中的触发器所取代。"两个过滤器吗?

在Ext JS组合框中删除临时项目的适当技术是什么?

在配置中使用lastQuery: "

我遇到了一个类似的问题,当第一次点击触发器时,组合框将显示所有项目,而不管过滤器是什么。

要确保在第一次使用ComboBox触发器时不会清除存储中的过滤器,请使用lastQuery= "http://docs.sencha.com/extjs/4.2.1/# !/api/Ext.form.field.ComboBox-property-lastQuery

您想了解如何重现triggerAction:'all'的行为,那么为什么不深入研究代码呢?

下面是类ComboBox的源代码:http://docs.sencha.com/ext-js/4-0/source/ComboBox.html Ext-form-field-ComboBox-cfg-triggerAction

如果你看一下代码,你会看到:

1)当触发器被点击时,方法doQuery被调用。

onTriggerClick: function() {
    var me = this;
    if (!me.readOnly && !me.disabled) {
        if (me.isExpanded) {
            me.collapse();
        } else {
            me.onFocus({});
            if (me.triggerAction === 'all') {
                me.doQuery(me.allQuery, true);
            } else {
                me.doQuery(me.getRawValue(), false, true);
            }
        }
        me.inputEl.focus();
    }
},
在doQuery方法中,有趣的代码是:
if (isLocalMode) {
    // forceAll means no filtering - show whole dataset.
    if (forceAll) {
        store.clearFilter();
    } else {
        // Clear filter, but supress event so that the BoundList is not immediately updated.
        store.clearFilter(true);
        store.filter(me.displayField, queryString);
    }
}

3)我们可以看到Store的方法过滤器被调用了。你有你的答案,适当的技术来删除临时项目在ExtJS组合框(一般在商店),是使用方法过滤器的商店。

http://docs.sencha.com/ext-js/4-0/# !/api/Ext.data.Store-method-filter

记住,你最好的朋友永远是文档!http://docs.sencha.com/ext-js/4-0/

你需要删除组合框的属性'lastQuery',每次你过滤商店。这就是ComboBox缓存的地方在它第一次建立它之后,它是Entryset。

所以像这样做:

      'window combobox[name=countryselection]' : {
            'change' : function(view, newValue){
                with(Ext.data.StoreManager.lookup('Subcountries')){
                    var combobox = Ext.getCmp('MainWindow').query('combobox[name=subcountryselection]')[0];
                    //force the combobox the rebuild its entryset
                    delete combobox.lastQuery;
                    clearFilter();
                    filter('countryId', newValue);
                }
            }
        }

它对我很有用:-)

请记住,筛选不会使用新数据"重新创建"存储,因此,如果您为"apple"筛选具有以下值的组合:

orange
banana
apple

点击触发器,就会显示"apple"。但是,如果您开始输入(并且激活了typeAhead: true),则基于您的输入的过滤将默认返回到orange/banana/apple Store。