3

So I have a list but I want to filter out the type if its '05' or '01' to not display in the list. The following code work fine for '05' but not '01'. How would I write it to add another value?

items="{
  path: 'Entries',
  filters: [{
    path: 'Type',
    operator : 'NE',
    value1 : '05',
    value2: '01'
  }]
}"

I tried this which I would assume is the logical way, but it then started displaying both 05 and 01. So I assume this is using an OR filter rather than filter AND filter combined.

items="{
  path:'Entries',
  filters: [{
    path: 'Type',
    operator: 'NE',
    value1: '05'
  }, {
    path: 'Type',
    operator: 'NE',
    value1: '01'
  }]
} 
Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
J0rd4n500
  • 223
  • 1
  • 5
  • 14
  • Does the [answer](https://stackoverflow.com/a/53561773/5846045) below solve the problem? Feel free to leave a comment there if something is unclear. – Boghyon Hoffmann Dec 04 '18 at 11:07

2 Answers2

6

Here is the syntax in XML view:

items="{
  path: '/Products',
  filters: [
    {
      filters: [
        {
          path: 'ProductName',
          operator: 'StartsWith',
          value1: 'Sir '
        },
        {
          path: 'Discontinued',
          operator: 'EQ',
          value1: false
        },
      ],
      and: true
    }
  ]
}"

Working example: https://embed.plnkr.co/wAlrHB?show=view/Home.view.xml,preview

The parameter filters awaits an array of filter info objects. A filter info object is constrained in the following combinations of properties:

  • path, operator, value1 (and value2 if applicable)
  • Or filters and and. <-- This is what we need.

API reference: sap.ui.model.Filter


The reason, why your second approach didn't work, is because both filters were pointing to the same path ('Type') which results in OR logic.

All filters applied to a single table column are ORed, while filters on different table columns are ANDed. Please either use the automatic grouping of filters (where applicable) or use explicit AND/OR filters, a mixture of both is not supported. [source]

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
  • can I have multiple filters? I need basically somehow to "create" an OR by having then two filters where I have a different value in the end – Nali Jul 25 '19 at 13:13
  • @Nali In the sample above, if you set `and: false`, then the two filter properties `ProductName` and `Discontinued` are performed with an OR. Is that what you wanted? – Boghyon Hoffmann Jul 25 '19 at 13:42
  • Yes from what you copied from the documentation the and: true in your original post is actually obsolete right? Because it is two different paths they are ANDed anyway. Now if they would be two times ProductName in your filter they would be ORed and if you want them to be ANDed you have to add your and: true did I understand this correctly? – Nali Jul 29 '19 at 08:53
  • To use the working example, go to the proxy "https://cors-anywhere.herokuapp.com/" and accept the temporary access to avoid CORS issues. After that, you can rerun the app in plnkr.co – uL1 Sep 02 '21 at 07:55
1

The value2 parameter to use with sap.ui.model.Filter is only valid with some sap.ui.model.FilterOperators such as BT. To work around this we can set up multiple sap.ui.model.FilterOperator.NE filters, in your case filtering the property "Type" for values 1 and 2.

By default if you combine many filters together, they are logically grouped with OR, that makes sense when you do the EQ filter but not for the NE filter where you have to use AND. Luckily the sap.ui.model.Filter class has a parameter that let's us define this.


In my example I have an sap.m.Table that has a binding to the following "Fruits" object:

var Fruits = {
   "Fruits": [{
     "id": "1",
     "name": "Apple"
    }, {
     "id": "2",
     "name": "Blueberry"
    }, {
     "id": "3",
     "name": "Cranberry"
    }]
};

And just like in your question, I want to filter both "id" 1 and 2 out of my table. This can be achieved by the following method:

var oTable = this.getView().byId("idListFruits");
oTable.getBinding("items").filter(new Filter({
   filters: [
     new Filter({
         filters: [
             new Filter("id", sap.ui.model.FilterOperator.NE, "1"),
             new Filter("id", sap.ui.model.FilterOperator.NE, "2")
         ], and: true
     })
   ]
}));

This way I can apply both NE filters to my table and also set the AND parameter to true indicating that an "AND" logical conjunction is applied on the filters.

My table output:

My fruits table without both ids 1 and 2[4]

Andre F
  • 526
  • 1
  • 3
  • 16
  • 1
    Thanks Andre, I knew I could do it above in the controller, I'm trying to find a simple solution to do it in a list items in xml. – J0rd4n500 Nov 30 '18 at 14:06