0

I have a collection of Person and in their object, I have a list of familymembers. Each family member is of type Person and they can also have their own list of familymembers. Here is a simplified object description in c#

public class Person 
{
    public string name {get;set;}
    public List<Person> familymembers {get;set;}
}

A example Json data would be:

[
    {
        "name": "Poppy",
        "familymembers": [null]
    },
    {
        "name": "John",
        "familymembers": [
            {
                "name": "Stephanie",
                "familymembers": [..]
            },
            {
                "name": "Tom",
                "familymembers": [..]
            },
            {
                "name": "Turner",
                "familymembers": [..]
            }
        ]
    }
]

Poppy doesn't have a collection of familymembers, but John does have. The dots represent another collection of familymembers of type Person.

With angular I would use ng-repeat-start to loop through collection of persons and I would use ng-repeat-end to iterate if I would find that the Person would contain a collection of family members:

The problem:

I really don't know what the depth of the collection would end with x many times. So instead of parse the data to HTML with ng-repeat I would like to use angular template or angular directive to loop x many times whether there would exist a collection of family members in every outer loop.

P.s. This is not a school project, this is just a very simplified version of my problem of knowing when I should iterate the sub-collection if it exist.

Can somebody point me out how they would implement this problem with rendering this kind of data?

Sincerly aghaux

R. Richards
  • 24,603
  • 10
  • 64
  • 64
aghaux
  • 729
  • 4
  • 14
  • 38
  • So you just want a recursive ngIf? – Austin T French Sep 04 '18 at 22:04
  • @aghaux You can find more information in: https://stackoverflow.com/questions/45448962/recursive-ng-repeat-x-times and https://stackoverflow.com/questions/37785885/recursion-with-ng-repeat-in-angular. – Danny Fardy Jhonston Bermúdez Sep 04 '18 at 23:13
  • Thank you, that gave me an idea how to solve recursive. But I need to be able to set a new table row if there is any subcollection. By using just ul and li the html renders it like a tree again and again. – aghaux Sep 05 '18 at 10:52

0 Answers0