sapui5 how to construct nested filters with AND and OR conditions

2.2k Views Asked by At

Hi i have an object with the data like

{
 status : [ "started", "completed"],
 branch : [ "India", "Germany" ]
}

here i want to construct nested filters like the status is either started OR completed AND the branch is either India OR Germany

i tried with the code below

var oFilter = new sap.ui.model.Filter(
   keys.forEach(function(v) {
     var oItem = totalFilterValues[v];
     if(v === "status"){
         return new sap.ui.model.Filter(oItem.forEach(function(val) {

              return new sap.ui.model.Filter("status",   
                  sap.ui.model.FilterOperator.Contains, val);

      }), false);
    } 

    if(v === "branch"){
         return new sap.ui.model.Filter(oItem.forEach(function(val) {

              return new sap.ui.model.Filter("branch",   
                  sap.ui.model.FilterOperator.Contains, val);

      }), false);
    } 

  }),true);

but was not working can you help me in this. i followed to the below code snippet.

    var oFilter = new sap.ui.model.Filter(mFacetFilterLists.map(function(oList) {
      return new sap.ui.model.Filter(oList.getSelectedItems().map(function(oItem) {
        return new sap.ui.model.Filter(oList.getTitle(), "EQ", oItem.getText());
      }), false);
    }), true);
1

There are 1 best solutions below

2
On

For multiple Filters:

var oStatusFilter = new sap.ui.model.Filter('status', function(oValue) {
    if (oValue === "started" || oValue === "completed")
        return true;
    else return false;
});
var oCountryFilter = new sap.ui.model.Filter('branch',
    function(oValue) {
        if (oValue === "India" || oValue === "Germany")
            return true;
        else return false;
    }
);
var oFilters = new sap.ui.model.Filter([oCountryFilter, oStatusFilter], true
});

Read more here SAPUI5 Filters