简体   繁体   English

搜索字段:过滤嵌套 object

[英]Search field: Filtering nested object

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.我的任务是通过pos数组中的属性过滤 JSON model 中的数据。 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.此外,我发现可以使用 ODataModel,但我没有找到任何关于 JSONModel 的信息。

Can such filtering be done at all?这样的过滤可以完成吗?

Thank you.谢谢你。

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:下面是一个在Filter的构造函数下测试function的例子: 过滤结果将返回一行包含过滤器中引入的materialDesc描述的列表:

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);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM