Openui5: Filter support for nested array (attributes)

Created on 17 Oct 2017  路  11Comments  路  Source: SAP/openui5

I am using the 1.48 version.
I have a list that is created based on a factory function (two different kind of cells)

Let's say the json like this (nested):

[
{ name: "a",age:"12"},
{ name: "b",age:"14"},
{ group: "groupB", people: {[ { name: "z",age:"14"}, { name: "s",age:"16"}] }},
{ name: "c",age:"16"},
{ group: "groupA", people: {[ { name: "e",age:"14"}, { name: "f",age:"16"}] }},
{ name: "d",age:"18"}
]

In the documentation there is no explanation for how to filter a nested result...

// returns entries without nested people
var oFilter = new Filter("name", FilterOperator.EQ, sName),

but how can I get the group back that contains also a Name value ?

// returns nothing
var oFilter = new Filter("people.name", FilterOperator.EQ, sName),
in progress

Most helpful comment

Hi @kyakubi ,

it's not very clear whether your filter is applied to a JSONModel or a ODataV2Model. Therefore I will explain a bit about the nested filtering on both models.

The OData Version 2 supports filtering on navigation property only when the navigation property is NOT a collection.

The URL example in a reply from @ThePlenkov works because the relation between "Address" and one "Suppliers" is 1:1 and not N:1.

/Suppliers?$filter=Address/City eq 'Redmond'

If the navigation property is a collection (like in your example), the OData Version 2 doesn't support filtering on its property. Please see details here: https://stackoverflow.com/a/33218540

The filtering on a JSONListBinding doesn't support the nested filtering either. You have to use a custom filtering function and filter the items manually as you have already mentioned in your last reply.

I didn't get it to work with the native filters. So I created a custom filter (need a documentation update on that because its really lacking...

So for anyone that wants to filter through a tree you can use something like this to filter...

let oGroupFilter = new Filter("", oNode => {
if(oNode.Group){
var match = oNode.Group.some(People=> {
if (People.name == sName){ return true;}
});
return match;
}
});

Best regards,
Jiawei

All 11 comments

The Filter translates the method calls to oData operator URL's that's why we can't rely on native filtering.
I can see it exists in oDATA V2 by adding a "/". I guess that doesnt exists in the Filter method ?

@kyakubi please check OData V4 Model: Filtering -
https://sapui5.hana.ondemand.com/#/topic/5338bd1f9afb45fb8b2af957c3530e8f

We have V2 oData...

@stephania87 As I understand, what @kyakubi wants is to use V2 filter like this:

/Suppliers?$filter=Address/City eq 'Redmond'

This URL is described in version 2.0:
http://www.odata.org/documentation/odata-version-2-0/uri-conventions/

@ThePlenkov thanks :)

what @ThePlenkov said is that possible with the Filter methods or do new ones need to be made ?

So its not possible I guess

Ok, let me try once again :D
Is the described here filtering is what you need based on V2 oDataModel and filters - https://stackoverflow.com/questions/39513124/ui5-odata-service-with-batch-get-with-filters
where you can also use the URL with $filter and a query.

I didn't get it to work with the native filters. So I created a custom filter (need a documentation update on that because its really lacking...

So for anyone that wants to filter through a tree you can use something like this to filter...

let oGroupFilter = new Filter("", oNode => {
   if(oNode.Group){
     var match = oNode.Group.some(People=> {
        if (People.name == sName){ return true;}
      });
      return match;
    }
});

@kyakubi The data structure does not have a tabular representation, that may be an issue. I am forwarding the question for further internal discussion with #1880001575, where updates will be posted here as well.

Hi @kyakubi ,

it's not very clear whether your filter is applied to a JSONModel or a ODataV2Model. Therefore I will explain a bit about the nested filtering on both models.

The OData Version 2 supports filtering on navigation property only when the navigation property is NOT a collection.

The URL example in a reply from @ThePlenkov works because the relation between "Address" and one "Suppliers" is 1:1 and not N:1.

/Suppliers?$filter=Address/City eq 'Redmond'

If the navigation property is a collection (like in your example), the OData Version 2 doesn't support filtering on its property. Please see details here: https://stackoverflow.com/a/33218540

The filtering on a JSONListBinding doesn't support the nested filtering either. You have to use a custom filtering function and filter the items manually as you have already mentioned in your last reply.

I didn't get it to work with the native filters. So I created a custom filter (need a documentation update on that because its really lacking...

So for anyone that wants to filter through a tree you can use something like this to filter...

let oGroupFilter = new Filter("", oNode => {
if(oNode.Group){
var match = oNode.Group.some(People=> {
if (People.name == sName){ return true;}
});
return match;
}
});

Best regards,
Jiawei

Was this page helpful?
0 / 5 - 0 ratings