6

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?

9 Answers 9

8

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)
]
3
  • Have tried this , but it always return a $batch requests must be POST error, any idea?
    – Tina Chen
    Jan 19, 2017 at 4:10
  • will you explain the complete scenario how you are getting this error. Is it during a oData call or only during setting the filter. Jan 19, 2017 at 9:49
  • Thank you ~ I've solved it ! it's because I used this in a datetime type data, how stupid ! I described it in here: stackoverflow.com/questions/41736210/…
    – Tina Chen
    Jan 19, 2017 at 9:56
4

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.

9
  • 1
    Hi, thank you Qualiture. But I've still problems. This is working: var oFilter1 = new Filter("name", FilterOperator.Contains, query); var list = this.getView().byId("list"); list.getBinding("items").filter([oFilter1], FilterType.Application); But with a 2nd, it isn't working anymore: var filter1 = new Filter("name", FilterOperator.Contains, query); var filter2 = new Filter("surname", FilterOperator.Contains, query); var list = this.getView().byId("list"); list.getBinding("items").filter([filter1, filter2], FilterType.Application); What is wrong here? Thanks, Alex Apr 17, 2014 at 11:14
  • 1
    Oh damn...I can not "ask" the question for 8 hours to have code blocks and the comment are very bad formatted.. Apr 17, 2014 at 11:15
  • 1
    Hi, can you try with .bindAggregation("items", { path : "/", filters : [oFilter1, oFilter2] });
    – Qualiture
    Apr 17, 2014 at 13:16
  • 1
    Hi, I think list.getBinding("items").filter([ oFilter1, oFilter2 ]); is working now, BUT with this the query have to be in both values (name and surname). Is there a way to provide a OR relationship and no AND ? Thanks, Alex Apr 17, 2014 at 14:02
  • 1
    Not sure if that's possible, and it's probably not even what a user expects; having multiple filters providing more results than having a single filter would provide...
    – Qualiture
    Apr 18, 2014 at 7:07
3

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.

1
  • This is correct! (Although it may seem wired to apply sap.ui.model.Filter on top of its own results.)
    – Jpsy
    Sep 28, 2018 at 11:48
3

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);
1

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);
4
  • Hey, thank you for your answer - but how can I detect, if the query is a name or a gender? Because like you wrote it, it shouldn't work - but I think the idea is not so bad. I just want to provide a search over multiple fields with one query. Apr 18, 2014 at 8:53
  • what exactly is there in this 'query' variable and how are you getting it?
    – Breakpoint
    Apr 18, 2014 at 9:43
  • the "query" variable is the value of the searchfield - or rather the searchquery text Apr 18, 2014 at 9:48
  • why dont you use a searchfield instead? use something similar to whats given here, openui5.hana.ondemand.com/#test-resources/sap/ui/commons/…
    – Breakpoint
    Apr 18, 2014 at 9:51
1

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')]  
});
1

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!

0
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}));
2
  • 3
    Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made.
    – Suraj Rao
    Jun 21, 2018 at 5:34
  • How does this answer differ from other answers suggesting the same? Jun 21, 2018 at 12:11
0

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)
]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.