Search field: Filtering nested object

691 Views Asked by At

Hi all,

I have the following data structure :

[    {
        "supplierCode": "supplier1",
        "supplierDesc": "supplier1Desc",
        "pos": [ {
                "poNum": "11111",
                "materialNum": "matNum11",
                "materialDesc": "matDesc11"
            },
            {  "poNum": "11112",
                "materialNum": "matNum22",
                "materialDesc": "matDesc22"}            
]  },
    {"supplierCode": "supplier2",
        "supplierDesc": "supplier2Desc",
        "pos": [ {
                "poNum": "22222",
                "materialNum": "matNum11",
                "materialDesc": "matDesc11"},
            {"poNum": "22223",
                "materialNum": "matNum22",
                "materialDesc": "matDesc22"}]
    }
]

My task is to filter data in JSON model by properties in pos array. I tried the following approach:

myList = this.getView().byId("myList");
var binding = myList.getBinding("items");
if (!query) {
binding.filter([]);
} else {
binding.filter([new sap.ui.model.Filter([
   new sap.ui.model.Filter("supplierCode", sap.ui.model.FilterOperator.Contains, query),
   new sap.ui.model.Filter("supplierDesc", sap.ui.model.FilterOperator.Contains, query),
   new sap.ui.model.Filter("pos/materialDesc", sap.ui.model.FilterOperator.Contains, query)
], false)]);
}

with no luck.

Also, I found out it is possible to do with ODataModel, but I didn't find anything regarding JSONModel.

Can such filtering be done at all?

Thank you.

1

There are 1 best solutions below

2
fareslt On

Here is an example on test function under the constructor of the Filter : Filter result will return a line of list containing the materialDesc description introduced in the filter :

onFilterInvoices: function(oEvent) {
            // build filter array
            var aFilter = [];
            var sQuery = oEvent.getParameter("query");
            if (sQuery) {
                aFilter.push(new sap.ui.model.Filter({
                    path: "pos",
                    test: function(oValue) {
                        var oMaterials = oValue;
                        for (var i in oMaterials) {
                            if (oMaterials[i].materialDesc === sQuery) {
                                return true;
                            }
                        }
                        return false;
                    }
                }));
            }

            // filter binding
            var oList = this.getView().byId("listapp");
            var oBinding = oList.getBinding("items");
            oBinding.filter(aFilter);
        }