Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SAPUI5: How to filter data with 2 or more values

Tags:

sapui5

I'm currently trying some stuff with/in SAPUI5 and I've implemented a very simple search like this:

    var filters = [];
    var query = evt.getParameter("query");
    if (query && query.length > 0) {
        var nameFilter = new sap.ui.model.Filter("name", sap.ui.model.FilterOperator.Contains, query);

        filters.push(nameFilter);
    }

    var list = this.getView().byId("list");
    var binding = list.getBinding("items");
    binding.filter(filters);

Now I have following issue: with this logic I can just search, or rather filter, by the name of a person. I've also some additional fields like age, gender, etc and I want to perform a search for the age or gender, too. So I've tried to create a 2nd filter, like "genderFilter", which is using the "gender" field. After this adding this 2nd filter with the .push() method to the filters[]..but this isn't working.

I've already tried to watch the documentation, watched different examples, tried different ways - but I'm helpless. Can please someone help me with this issue?

like image 341
user3544563 Avatar asked Apr 17 '14 09:04

user3544563


2 Answers

For the requirement this code will work.

var list = this.getView().byId("list");
var binding = list.getBinding("items");
if( !query ) {
    binding.filter( [] );
} 
else {
   binding.filter( [ new sap.ui.model.Filter([
      new sap.ui.model.Filter("name", sap.ui.model.FilterOperator.Contains, query ),
      new sap.ui.model.Filter("gender", sap.ui.model.FilterOperator.Contains, query )
   ],false)
]
like image 101
Saddamhussain Avatar answered Oct 05 '22 07:10

Saddamhussain


According to the API

For manual filtering you should always pass the FilterType

If you change your code to

 list.getBinding("items").filter(filters, sap.ui.model.FilterType.Application);

it should work.

See also https://openui5.hana.ondemand.com/docs/guide/BindingAggregations.html at the very bottom.

like image 35
Qualiture Avatar answered Oct 05 '22 07:10

Qualiture