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?
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)
]
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With