Grayden Hormes
Grayden Hormes

Reputation: 875

UI5 filter for multiple values in the same column

I am trying to read from an odata model using all the selected items from my table. The table has two keys, I called them ID and NAME, it could be that i only need filters for one of the keys. Using only 1 filter did not change the result.

I then create filters for all of the items that are selected. Later it reads from the model using the filters but always comes back with 1 results, the first entry in the table.

I found here that you can make another filter using the filter array to do AND or OR on the filters, so I tried that but nothing changed.

var table = this.byId("table");
var items = table.getSelectedItems();
var filters = [];
for (var i = 0; i < items.length; i++) {
    var item = items[i].getBindingContext().getObject();
    var id = new Filter("ID", FilterOperator.Contains, item.ID);
    var name = new Filter("NAME", FilterOperator.Contains, item.NAME);
    filters.push(id);
    filters.push(name);
}

var model = this.getOwnerComponent().getModel();
model.read("/assessments", {
    filters: [new sap.ui.model.Filter(filters, false)],
    success: function(odata) {
        var results = odata.results;
    }
}

Upvotes: 0

Views: 4041

Answers (1)

Stephen S
Stephen S

Reputation: 3994

From what I could understand from you question, I believe you want to achieve as below

Row 1 - "ID" AND "NAME"

  • OR

Row 2 - "ID" AND "NAME"

  • OR

Row 3 - "ID" AND "NAME"

You could do that by setting the and argument in the parameter object in a nested filter.

var table = this.byId("table"),
    items = table.getSelectedItems(),
    aFilters = [];
for (var i = 0; i < items.length; i++) {
    var item = items[i].getBindingContext().getObject();
    var oFilter = new Filter({
        filters:[
            new Filter("ID", FilterOperator.Contains, item.ID),
            new Filter("NAME", FilterOperator.Contains, item.NAME)
        ],
        and : true
    })
    aFilters.push(oFilter);
}

var model = this.getOwnerComponent().getModel();
model.read("/assessments", {
    filters: [new sap.ui.model.Filter(aFilters, false)],
    success: function(odata) {
        var results = odata.results;
    }
}

Upvotes: 1

Related Questions