无法过滤多个extjs网格列

时间:2012-12-03 08:34:19

标签: extjs filter grid extjs4 store

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

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?

1 个答案:

答案 0 :(得分:2)

根据您实现过滤器的方式,我猜您正在使用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 with textfield

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