0

I am working with angularjs and I have this response from my service and Its coming as tree and I am representing that data to the angular tree. this object have 4 main children, name and id. but these children and their children also have children, name and id. What I want is no matter which child I click, I always want to access the value of main root parent name thats "node1" "node2" "node3" as name so i know what is my main parent and whats parent under im clicking.

$scope.loadOrganizationTree = function () {
     userManagementFactory.getOrganizationTree()
     .success(function(data, status) {
        if (JSON.stringify(data.statusType).indexOf("success") > -1) {
            $scope.dataForTheTree = data.statusMsg;     
                }
        } else {
            $scope.setResponseMsgs(data, status);
        }
    }).error(function(data, status) {
        $scope.userErrorMsg = data.statusMsg;
        $scope.showErrorMSg = true ;
    });
<div treecontrol class="tree-light accordion-org-tree"
     style="height: 609px !important;" tree-model="dataForTheTree"
     order-by="name"  reverse-order="false"  options="treeOptions"
     on-selection="showSelected(node, selected, $parentNode, $index, $first, $middle, $last, $odd, $even)"
     selected-node="node1" filter-expression="userProfileOrgSearch">

    {{node.name}} 
</div>
[
  {
    "id": 1,
    "name": "node1",
    "children": [
      {
        "id": 11,
        "name": "node1.1",
        "children": [
          {
            "id": 111,
            "name": "node1.1.1",
            "children": []
          }
        ]
      },
      {
        "id": 12,
        "name": "node1.2",
        "children": []
      }
    ]
  },
  {
    "id": 2,
    "name": "node2",
    "children": [
      {
        "id": 21,
        "name": "node2.1",
        "nodes": []
      },
      {
        "id": 22,
        "name": "node2.2",
        "nodes": []
      }
    ]
  },
  {
    "id": 3,
    "name": "node3",
    "children": [
      {
        "id": 31,
        "name": "node3.1",
        "children": []
      }
    ]
  }
]

So far I have tried accessing with $parentnode but its not working out for me. Please let me know the solution. Thanks in advance.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Viraj
  • 27
  • 7
  • The `.success` and `.error` methods have been [removed from the AngularJS framework](https://stackoverflow.com/questions/35329384/why-are-angularjs-http-success-error-methods-deprecated-removed-from-v1-6/35331339#35331339). – georgeawg Feb 18 '20 at 02:42
  • it’s an old code. and that’s not even problem. Thank you for the update though :) – Viraj Feb 18 '20 at 04:10

1 Answers1

0

After loading the data you fill $scope.dataForTheTree. Then you can walk through the tree and set the root of each node. I coded this for you using a var dataForTheTree with your example data. After running the below, each node has a root property set with it's root node.

var setRoot = function(elementWithChildren, root) {
    elementWithChildren.root = root;
    if (elementWithChildren.children != null) {
        angular.forEach(elementWithChildren.children, function(child) { setRoot(child, root); });
    }
};
angular.forEach(dataForTheTree, function(rootNode) { setRoot(rootNode, rootNode); });

we take each node of the tree and call the setRoot() function. which then calls the setRoot function for each of it's children. Recursive.

PeterFromCologne
  • 10,213
  • 9
  • 36
  • 46