在两个不同的列

时间:2015-11-06 14:52:55

标签: javascript sorting extjs

我试图在两列上过滤商店,一列有一个数组,另一列有一个单词的开头(它将是AND,而不是OR)。 所以我知道如何使用数组过滤商店:

 grid.store.load().filterBy(function(record, id){
                return Ext.Array.indexOf(filter, record.get("type")) !== -1;
            }, this);

我知道如何使用单词的开头过滤商店:

 {id: 'name', property: "name", value: newValue, anyMatch: true}

但我不知道如何将两者结合起来。

我试过了:

        grid.store.load().filterBy(function(record, id){
            if(Ext.Array.indexOf(filter, record.get("type")) !== -1 && record.get("name").indexOf(newValue) !== -1){
                return record;
            }

        }, this);

但是当我只想要匹配使其有效时,它会返回整个单词。如何捕捉到部分内容?

2 个答案:

答案 0 :(得分:1)

通过在每次过滤时创建新过滤器,可以在模型的多个字段上过滤存储。可以通过将anyMatch设置为true或使用正则表达式来过滤部分单词。

过滤一列(模型的一个字段)

这里我创建了一个使用anyMatch过滤网格列的示例。

https://fiddle.sencha.com/#fiddle/ts4

列上方的部分文本字段代码(示例应用中的FilterField):

keyup: {
    fn: function(field, event, eOpts) {
        var grid = this.up('grid'),
            /* only filter when value is empty and key is backspace or delete */
            isValidKey = ((event.keyCode === Ext.event.Event.BACKSPACE || event.keyCode === Ext.event.Event.DELETE) || !event.isSpecialKey());

        if(isValidKey === true) {
            grid.getStore().filter(new Ext.util.Filter({
                anyMatch: true,
                disableOnEmpty: true,
                property: field.up('gridcolumn').dataIndex,
                value   : field.getValue()
            }));

            grid.getSelectionModel().select(0);
        }
    },
    buffer: 250
}

过滤所有列(模型的所有文件)

这里我创建了一个示例,它使用正则表达式对网格的所有列进行过滤。

https://fiddle.sencha.com/#fiddle/un7

控制器的一部分代码(MainController):

filterStore: function(searchValue) {
    var me = this,
        grid = me.getMainGrid(),
        store = grid.getStore(),
        regex = RegExp(searchValue, 'i');

    store.clearFilter(true);

    store.filter(new Ext.util.Filter({
        filterFn: function(record) {
            var match = false;
            Ext.Object.each(record.data, function(property, value) {
                match = match || regex.test(String(value));
            });
            return match;
        }
    }));
}

答案 1 :(得分:0)

我用正则表达式解决了它。 如果有人想要使用数组和值来过滤2列上的网格,请按以下步骤操作:

grid.store.load().filterBy(function(record, id){
                var re = new RegExp("/(^"+newValue+" )", 'i');
                if(Ext.Array.indexOf(filter, record.get("type")) !== -1 && record.get("name").match(re) !== -1){
                    debugger;
                    return record;
                }

            }, this);
相关问题