Reputation: 61
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?
Upvotes: 6
Views: 105136
Reputation: 130
Simple way to bind two or more filters.
var sQuery = oEvent.getParameter("value");
var oBinding = oEvent.getSource().getBinding("items");
oBinding.filter([
new sap.ui.model.Filter("column A", sap.ui.model.FilterOperator.Contains, sQuery),
new sap.ui.model.Filter("column B", sap.ui.model.FilterOperator.Contains, sQuery)
]);
Upvotes: 0
Reputation: 1
var afilters = [];
var query = evt.getParameter("query");
afilters.push(new sap.ui.model.Filter("name", sap.ui.model.FilterOperator.Contains, query);
afilters.push(new sap.ui.model.Filter("age", sap.ui.model.FilterOperator.Contains, query);
var list = this.getView().byId("list");
var binding = list.getBinding("items");
binding.filter(new sap.ui.model.Filter({filters: afilters, and: true|false}));
Upvotes: 0
Reputation: 51
I had to use both. So at the end this worked for me.
var oFilters = new sap.ui.model.Filter({
filters: [
oFilter,
oFilter2
],
and: false
});
evt.getSource().getBinding("items").filter(oFilters, sap.ui.model.FilterType.Application);
Thanks for the help!
Upvotes: 1
Reputation: 31
I achieved this with the below code:-
var oFilter = new sap.ui.model.Filter("name",sap.ui.model.FilterOperator.Contains,searchString);
var oFilter1 = new sap.ui.model.Filter("ID",sap.ui.model.FilterOperator.Contains,searchString);
var comFil = new sap.ui.model.Filter([oFilter,oFilter1]);
var oList = sap.ui.getCore().byId("dealerList");
oList.getBinding("items").filter(comFil,sap.ui.model.FilterType.Application);
Upvotes: 3
Reputation: 79
to combine multiple filters you've to write the filter this way:
new sap.ui.model.Filter({
filters: [
new sap.ui.model.Filter(col1, , ,val),
new sap.ui.model.Filter(col2, , ,val)
],
and: false
})
Source: https://sapui5.hana.ondemand.com/docs/api/symbols/sap.ui.model.Filter.html#constructor
Then you can filter in multiple columns with OR.
Upvotes: 3
Reputation: 870
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)
]
Upvotes: 8
Reputation: 6812
I hope I got everything right: Here is how filtering more than one column when binding data to a table:
oTable.bindRows({
path : "/modelData",
filters: [new sap.ui.model.Filter("severity", sap.ui.model.FilterOperator.EQ, '2'),
new sap.ui.model.Filter("severity", sap.ui.model.FilterOperator.EQ, '3')]
});
Upvotes: 1
Reputation: 1541
Have only one filter in the filters
array for each of the criteria and it should work,
var filters = [];
var sFilter;
var query = evt.getParameter("query");
if (query && query.length > 0) {
if(query == "name" )
{
sFilter = new sap.ui.model.Filter("name", sap.ui.model.FilterOperator.Contains, query);
}
else if(query == "gender")
{
sFilter = new sap.ui.model.Filter("gender", sap.ui.model.FilterOperator.Contains, query);
}
//and so on...
filters.push(sFilter);
}
var list = this.getView().byId("list");
var binding = list.getBinding("items");
binding.filter(filters);
Upvotes: 1
Reputation: 4920
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.
Upvotes: 4