1

Fiddle : http://jsfiddle.net/p35cobgx/3/

Below is my tree structure :

Node
   Node-1
     Node-1-1......

Now I want to store back reference of all parents for each of the nodes like below :

Node

Node-1 (should store back reference of only Node because this is last parent for Node-1)

Node-1-1(should store back reference of all parents i.e Node-1 and Node because Node is the last parent for Node-1-1).

But problem with my code is i am unable to store reference of Node for Node-1-1.

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
I Love Stackoverflow
  • 6,738
  • 20
  • 97
  • 216
  • Not clear what you're asking. If Node-1-1 has a reference to Node-1 and Node-1 has a reference to Node, then Node-1-1 already has a reference to Node by simply looking at Node-1's parent. Why store it again? – Mark Aug 26 '17 at 09:15
  • This example might help you (2nd Demo): https://stackoverflow.com/questions/19691917/how-do-display-a-collapsible-tree-in-angularjs-bootstrap/19692791#19692791 – Maxim Shoustin Aug 26 '17 at 10:05
  • @MaximShoustin Thank you for the reference but i dont see where you are storing reference to all the parents in that answer.Can you give me some insights? – I Love Stackoverflow Aug 26 '17 at 11:13

3 Answers3

1

I'm not sure if that's exactly the answer you wanted, but here it is:

You can access a parent node by using:

node.parentNode

This is a property and holds the reference for the parent node.

Also, it's sometimes better to access parent element, instead of node, because whitespace can also be a node, so you can use this property:

node.parentElement
1

The algorithm I would use is to store only the immediate parent in the node.

So if A is the parent to B and B of C

That makes it A->B->C

C.parent = B; B.parent = A;

If I wanted all parents of C, I would do the below

    var parentArray = getParent(C).slice();
    this.parents = [];

    //use parentArray as you like now!


    function getParent(obj){

    if(obj.parent){
    this.parents.push(obj.parent) //this.parents is an array
    getParent(obj.parent);
    }else{
    return this.parents;
    }
    }

So if you make a call getParent(C); you get all its parents B and A. If you make a call getParent(B); you get only A. If you make a call getParent(A); you get no parents as it doesn't have any parents.

Hope that helps!

Amjad
  • 11
  • 3
  • Thank you for the answer but where does parents come here in this code : this.parents.push(obj.parent).Is it this.nodes.push(obj.parent)?? – I Love Stackoverflow Aug 26 '17 at 11:15
  • this.parents is just an array that you can use directly. Its an array on object. Mind you, the assumption in the above code is that you have already set the parent hierarchy between objects i.e. you have set C.parent = B.parent and B.parent = A.parent. Feel free to ask more questions. – Amjad Aug 26 '17 at 14:58
  • I also just made more changes in the code above to give you more clarity of what you could do. – Amjad Aug 26 '17 at 15:08
1

Use this condition for assigning the parent:

obj.parent = data.parent || null;

quirimmo
  • 9,800
  • 3
  • 30
  • 45