1

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

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);

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

这没有帮助如何过滤多个 extjs 网格列?

4

1 回答 1

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

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

于 2012-12-03T08:45:43.153 回答