Tina Chen
Tina Chen

Reputation: 2030

How to concatenate two sap.ui.model.Filter in JSON model?

I have a JSON model like this:

{
    "VEHICLES": [
        {
            "vehicleId": "0001",
            "routeName": "Ginza Line"
        },
        {
            "vehicleId": "0002"
            "routeName": "Another Line"
        }
}

I am implementing a sort function like this:

onSearch : function (oEvent) {
    var sQuery = oEvent.getParameter("query");

    if (sQuery) {
        var oFilter1 = new Filter("vehicleId", function(value) {
            return value.includes("sQuery");
        });

        var oFilter2 = new Filter("routeName", function(value) {
            return value.includes("sQuery");
        });

        var allFilter = new Filter([oFilter1, oFilter2], false});
        var list = this.getView().byId("masterList");
        list.getBinding("items").filter(allFilter);
    } 
}

When I input "000" in search field, I am expecting to return both data items, but it returns none. Why?

I have tried single "vehicleId" filter, it worked.

I have also tried

var allFilter = new Filter({
    filters: [oFilter1, oFilter2], 
    and: false  //OR
});

But allFilter.bAnd returned undefined, I am confused.

Ref:

https://openui5.hana.ondemand.com/#docs/api/symbols/sap.ui.model.Filter.html

https://openui5.hana.ondemand.com/#docs/guide/96804e3315ff440aa0a50fd290805116.html#loio50357d719b35439984348080bcbb14a4

https://github.com/SAP/openui5/issues/1442

Upvotes: 1

Views: 2443

Answers (3)

Haojie
Haojie

Reputation: 5713

Update:

according to API document. Put the parameter to Array

list.getBinding("items").filter([allFilter]);

@boghyon found from the source code here that it is actually got converted to Array

Upvotes: 0

Tina Chen
Tina Chen

Reputation: 2030

Thanks to @boghyon and @suryabhan mourya, I changed the fnTest function to

var oFilter1 = new Filter("vehicleId", function(value) {
    return value.includes(sQuery.toUpperCase());
});

and it's working!

Upvotes: 0

Boghyon Hoffmann
Boghyon Hoffmann

Reputation: 18044

There is a small mistake. You are checking if the vehicleId includes the string "sQuery". Remove the quotes around sQuery and the filters will work fine.

value.includes(sQuery);

Upvotes: 1

Related Questions