openui5 icon indicating copy to clipboard operation
openui5 copied to clipboard

Filter support for nested array (attributes)

Open kyakubi opened this issue 9 years ago • 11 comments

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),

kyakubi avatar Oct 17 '17 14:10 kyakubi

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 avatar Oct 20 '17 09:10 kyakubi

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

stephania87 avatar Oct 20 '17 13:10 stephania87

We have V2 oData...

kyakubi avatar Oct 20 '17 14:10 kyakubi

@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 avatar Oct 20 '17 14:10 ThePlenkov

@ThePlenkov thanks :)

stephania87 avatar Oct 22 '17 12:10 stephania87

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

kyakubi avatar Oct 23 '17 07:10 kyakubi

So its not possible I guess

kyakubi avatar Nov 13 '17 15:11 kyakubi

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.

stephania87 avatar Nov 22 '17 08:11 stephania87

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 avatar Nov 27 '17 09:11 kyakubi

@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.

stephania87 avatar Jan 02 '18 10:01 stephania87

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

stopcoder avatar Jan 02 '18 13:01 stopcoder