【问题标题】:Can't filter multiple extjs grid columns无法过滤多个 extjs 网格列
【发布时间】:2012-11-20 16:55:26
【问题描述】:

要过滤我使用的一个网格列:

store.filter({
          property: 'first_name',
          anyMatch: true,
          value   : this.getValue()
      });

现在我需要一次搜索多个字段,例如:

      var filters = [
    new Ext.util.Filter({
    property: "first_name", anyMatch: true, value: this.getValue()
    }),
    new Ext.util.Filter({
   property: "last_name", anyMatch: true, value: this.getValue()
   })
 ];
  store.filter(filters);

奇怪的是,在这两种情况下,只有单一搜索有效

这没有帮助How to filter multiple extjs grid columns?

【问题讨论】:

    标签: extjs filter grid extjs4 store


    【解决方案1】:

    根据您实现过滤器的方式,我猜您正在使用remoteSort: false 作为提示:您不需要创建过滤器的实例,只需提供配置即可。如果可能的话备用this。大多数事件提供组件的实例,使用它而不是this。它可以让你头疼。

    我测试了它并且它有效。这是一个简单的例子:

    Ext.create('Ext.data.Store', {
        storeId:'simpsonsStore',
        fields:['name', 'email', 'phone'],
        data:{'items':[
            { 'name': 'Lisa',  "email":"lisa@simpsons.com",  "phone":"555-111-1224"  },
            { 'name': 'Lisam',  "email":"lisa@simpsons.com",  "phone":"555-222-1234" },
            { 'name': 'Bart',  "email":"bart@simpsons.com",  "phone":"555-222-1234" },
            { 'name': 'Homer', "email":"home@simpsons.com",  "phone":"555-222-1244"  },
            { 'name': 'Marge', "email":"marge@simpsons.com", "phone":"555-222-1254"  }
        ]},
        proxy: {
            type: 'memory',
            reader: {
                type: 'json',
                root: 'items'
            }
        }
    });
    
    Ext.create('Ext.grid.Panel', {
        title: 'Simpsons',
        tbar: [{
            text: 'filter',
            handler: function(btn) {
                var g = btn.up('grid');
                g.store.filter([{property: "name", anyMatch: true, value: 'Lisa'},{property: "email", anyMatch: true, value: 'lisa@simpsons.com'}])
            }
        }],
        store: Ext.data.StoreManager.lookup('simpsonsStore'),
        columns: [
            { text: 'Name',  dataIndex: 'name' },
            { text: 'Email', dataIndex: 'email', flex: 1 },
            { text: 'Phone', dataIndex: 'phone' }
        ],
        height: 200,
        width: 400,
        renderTo: Ext.getBody()
    });
    

    这是一个JSFiddle

    第 2 版 带有文本字段

    Ext.create('Ext.data.Store', {
        storeId:'simpsonsStore',
        fields:['name', 'email', 'phone'],
        data:{'items':[
            { 'name': 'Lisa',  "email":"lisa@simpsons.com",  "phone":"555-111-1224"  },
            { 'name': 'Lisam',  "email":"lisa@simpsons.com",  "phone":"555-222-1234" },
            { 'name': 'Bart',  "email":"bart@simpsons.com",  "phone":"555-222-1234" },
            { 'name': 'Homer', "email":"home@simpsons.com",  "phone":"555-222-1244"  },
            { 'name': 'Marge', "email":"marge@simpsons.com", "phone":"555-222-1254"  }
        ]},
        proxy: {
            type: 'memory',
            reader: {
                type: 'json',
                root: 'items'
            }
        }
    });
    
    Ext.create('Ext.grid.Panel', {
        title: 'Simpsons',
        tbar: [{
            xtype: 'textfield',
            emptyText: 'filter',
            listeners: {
                specialkey: function(field, e){
                    if (e.getKey() == e.ENTER) {
                        var g = field.up('grid'),
                            value = field.getValue(); 
                        g.store.filter({scope: this, filterFn: function(rec) { 
                        var rege = new RegExp(".*" + value + ".*"); 
                        if (rege.test(rec.data.name) || rege.test(rec.data.email)) {
                            return true;
                        }
                        return false;
                    } 
                });
                    }
                }
            }
        }],
        store: Ext.data.StoreManager.lookup('simpsonsStore'),
        columns: [
            { text: 'Name',  dataIndex: 'name' },
            { text: 'Email', dataIndex: 'email', flex: 1 },
            { text: 'Phone', dataIndex: 'phone' }
        ],
        height: 200,
        width: 400,
        renderTo: Ext.getBody()
    });​
    

    还有 JSFiddle

    【讨论】:

    • 好的,当我使用 :store.filter([{property: "first_name", anyMatch: true, value: this.getValue()},{property: "last_name", anyMatch: true, value: this.getValue()}]); 时,它可以工作,但作为 AND 条件,例如当用户键入“ma”时......名字。我需要它是或。我怎样才能做到这一点 ?非常感谢。
    • @Noon 您必须为此编写自定义 filterFn,因为过滤器始终与 AND 连接。一有时间我会更新我的帖子
    • @Noon 查看我的编辑,现在也使用文本字段(点击返回搜索)
    • 太棒了!现在我只需要 OR 条件
    • @Noon 它已经在那里了。看; if (rege.test(rec.data.name) || rege.test(rec.data.email)) 测试组合为OR,因此只需满足一个条件,您就会得到结果。
    猜你喜欢
    • 2012-06-04
    • 1970-01-01
    • 2020-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-23
    相关资源
    最近更新 更多