Ahmed
Ahmed

Reputation: 43

Problem in adding multi filter to odata service

I have the following code. I am trying to read 3 different values for PO from input fields then displaying the result in list. Program is working fine for single input but for multiple inputs i am facing issues.

var oV1 = this.getView().byId("oInput").getValue();
var oV2 = this.getView().byId("oInput1").getValue();
var oV3 = this.getView().byId("oInput2").getValue();                                     


var oFilter = [new sap.ui.model.Filter("Ebeln", sap.ui.model.FilterOperator.Contains, oV1)];          
var oFilter1 = [new sap.ui.model.Filter("Ebeln", sap.ui.model.FilterOperator.Contains, oV2)];
var oFilter2 = [new sap.ui.model.Filter("Ebeln", sap.ui.model.FilterOperator.Contains, oV3)];



var orFilter =new Array(new sap.ui.model.Filter({filters:[oFilter, oFilter1, oFilter2],and:true}));

   var oView1 = this.getView();
   var oTable = oView1.byId("myTable");
   var oBinding = oTable.getBinding("items");
   if(oV1 === "")
   {
       oBinding.filter( [] );
       oBinding.refresh(true);
   }
   else
   {
     oBinding.filter(orFilter);

At the above oBinding.filter I am receiving following error. Filter in Aggregation of Multi filter has to be instance of sap.ui.model.Filter -

Unable to get property 'replace' of undefined or null reference

Please help.

Upvotes: 0

Views: 1232

Answers (2)

Marc
Marc

Reputation: 6190

You wrapped your filters in 1000 layers of Arrays which doesn't make sense.

Just create a single array which contains Filter objects:

// Filter, FilterOperator, and FilterType required from "sap/ui/model/*"
const aFilter = [
  new Filter("Ebeln", FilterOperator.Contains, sV1),
  new Filter("Ebeln", FilterOperator.Contains, sV2),
  new Filter("Ebeln", FilterOperator.Contains, sV3),
];
oListBinding.filter(aFilter, FilterType.Application);

I know this isn't a code review but some suggestions:

  • No one uses new Array(). Just use [].

  • Also you used Hungarian notation but every one of your variable names begins with o. o means object. Some of your variables are not objects but strings (like oV1, better would be sV1) or arrays.

Upvotes: 2

Johann Goltz
Johann Goltz

Reputation: 154

For the filters aggregation, sap.ui.model.Filter accepts a Filter[]. But because oFilter already is a Filter[], [oFilter, oFilter1, oFilter2] is a Filter[][], so that won't work.

To make it work, just remove the surrounding [] from the filter definitions, like so: var oFilter = new Filter(...);.

Upvotes: 1

Related Questions