How to filter multiple extjs grid columns?

Davor Zubak picture Davor Zubak · Nov 3, 2011 · Viewed 20.6k times · Source

To filter one grid column we can use:

{
     xtype: 'button',
     text:'Search',
     handler:function(){

        store.clearFilter();
        var searchValue = Ext.getCmp("textFieldId").getValue();
        store.load().filter('GridFieldName', searchValue);

     }                   
}

but how to search multiple fields at once, something like:

{
    xtype: 'button',
    text:'Search',
    handler:function(){

        store.clearFilter();
        var searchValue = Ext.getCmp("textFieldId").getValue();
        store.filter([
           {property: "GridFieldName", value: searchValue},
           {property: "GridFieldName1", value: searchValue}
        ]);
    }                   

}

any ideas?

EDIT:

The weird thing is that in both cases, only single search works:

This works:

store.filter([
      { property: "FirstName", value: searchValue }
]);

and this works:

var FirstNameFilter = new Ext.util.Filter({
   property: "FirstName", value: searchValue
});

store.filter(FirstNameFilter);

but this does not:

store.filter([
      { property: "FirstName", value: searchValue },
      { property: "LastName", value: searchValue }
]); 

or does this:

 var filters = [
     new Ext.util.Filter({
          property: "FirstName", value: searchValue
     }),
     new Ext.util.Filter({
          property: "LastName", value: searchValue
     })
 ];
 store.filter(filters);

Answer

Pavel Podlipensky picture Pavel Podlipensky · Nov 3, 2011

Try to create instances of Ext.util.Filter like this:

var filters = [
 new Ext.util.Filter({
  property: "GridFieldName", value: searchValue
 }),
 new Ext.util.Filter({
  property: "GridFieldName1", value: searchValue
 })
];
store.filter(filters);

Alternatively, you can create single filter with custom logic:

var filters = [
     new Ext.util.Filter({
      filterFn: function(item){
         return item.get('GridFieldName') == searchValue && item.get('GridFieldName1') == searchValue;
      }
     })
];
store.filter(filters);