SAPUI5GUY
SAPUI5GUY

Reputation: 71

Filtering table checks multiple columns

I have a table with several columns and also a search bar at the top that performs the function below. I thought that given the code below, only the "field1" column would be searched, but in fact if I search for a term in another column it will show up. Now my problem is there is a new column I'm adding that I'd like to add to this search. I try adding it to the filters array like this

new filter("fieldIWant",FilterOperator.Contains,sValue)

but it won't work. I made the column invisible because I don't want it displayed on the table but I want it to still be searchable. I guess my first question is why the search bar works for multiple fields when I only specified "field1".

onSearchPressed : function() {
    var sValue = this.byId("searchField").getValue();
    var aFilters = [];

    var filters = [new Filter("field1","EQ",sValue)];
    aFilters = filters;
    var oFilter = new Filter({aFilters : filters});
    if (!aFilters) {
        aFilters = [];
    }
    this._aSearchFilters = aFilters;
    if (this._aSelectFilters)   {
        aFilters = aFilters.concat(this._aSelectFilters);
    }
    if (this._aQuickFilters)    {
        aFilters =aFilters.concat(this._aQuickFilters); 
    }

    var oBinding = this.byId("catalogTable").getBinding("items");
    oBinding.filter(aFilters );
},

NOTE: This was prewritten code and isn't mine so I don't want to make any major changes but rather understand why it works like it does. _aSelectFilters and _aQuickFilters do not contain any searches for columns, they're for something else.

Upvotes: 0

Views: 384

Answers (2)

Developer
Developer

Reputation: 361

You may want to check your metadata file; look at the sap:filterable tag. If it's set to false and you are still able to filter then you have to check the backend. If they are set to true then the way you've done it using multiple new Filter(...) works fine.

Upvotes: 1

Sri ram
Sri ram

Reputation: 90

Hope this will help you

onSearchPressed : function() { 
var    sValue = this.byId("searchField").getValue();
 var        aFilters = [];
var searchFilter1 = new     sap.ui.model.Filter("key_of_column1",sap.ui.model.FilterOperator.Contains, sValue);
var searchFilter2 = new sap.ui.model.Filter("key_of_column2",sap.ui.model.FilterOperator.Contains, sValue);
var searchFilter3 = new sap.ui.model.Filter("key_of_column3",sap.ui.model.FilterOperator.Contains, sValue);
//Add as many columns you have
aFilters.push(searchFilter1);
aFilters.push(searchFilter2);
aFilters.push(searchFilter3);

var table = this.byId("catalogTable");
       table.getBinding("items").filter(aFilters);

Upvotes: 0

Related Questions