J0rd4n500
J0rd4n500

Reputation: 223

Multiple Filters in XML

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'
  }]
} 

Upvotes: 3

Views: 3595

Answers (2)

Boghyon Hoffmann
Boghyon Hoffmann

Reputation: 18064

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]

Upvotes: 6

Andre F
Andre F

Reputation: 534

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]

Upvotes: 1

Related Questions