dont know nothing dont know nothing - 3 months ago 120
AngularJS Question

angular treeview- get parent node

I am using angular treeview to build a tree using angularjs. Once I select a node, I want to know the 'parent node' of the selected one.

I don't want to add an attribute named 'parent' in the json data and also don't want to create any array of parent-child key-value pair.

What changes can be done in initial files to get the result?

HTML

<div ng-app="myApp">
<div ng-controller="myController">

<div style="display:table-row; width:500px;">
<div style="display:table-cell; width:250px;">
<div style="margin:10px 5px 30px 5px; padding:10px; background-color:#EEEEEE; border-radius:5px; font:12px Tahoma;">
<span><b>First Tree's Selected Node</b> : <br>{{tree01.currentNode.roleName}}</span>
</div>
</div>
<div style="display:table-cell; width:250px;">
<div style="margin:10px 5px 30px 5px; padding:10px; background-color:#EEEEEE; border-radius:5px; font:12px Tahoma;">
<span><b>Second Tree's Selected Node</b> : <br>{{tree02.currentNode.roleName}}</span>
</div>
</div>
</div>
<div style="display:table-row; width:500px;">
<div style="display:table-cell; width:250px;">

<!-- FIRST TREE -->
<div data-angular-treeview="true" data-tree-id="tree01" data-tree-model="roleList1" data-node-id="roleId" data-node-label="roleName" data-node-children="children">
</div>


</div>
<div style="display:table-cell; width:250px;">

<!-- SECOND TREE -->
<div data-angular-treeview="true" data-tree-id="tree02" data-tree-model="roleList2" data-node-id="roleId" data-node-label="roleName" data-node-children="children">
</div>
</div>
</div>
</div>
</div>


Here's the working fiddle

Answer

Try these changes.

In Markup: (add ng-click attribute)

 <div
    data-angular-treeview="true"
    data-tree-id="tree01"
    data-tree-model="roleList1"
    data-node-id="roleId"
    data-node-label="roleName"
    data-node-children="children" 
    ng-click="printParent($event);">
</div>

In Controller

$scope.printParent = function ($event) {
  var root = $scope;
  var currentScope = angular.element($event.target).scope();
  console.log('selected Node details: ', currentScope.node);
  currentScope = currentScope.$parent;
  console.log('parents::')
  while(currentScope.$id !== root.$id) {
    console.log(currentScope.node);
    currentScope = currentScope.$parent;
  }
}

Working Fiddle

To access the parent in the directive:

Click Handler

scope[treeId].selectNodeLabel = 
    function( selectedNode, selectedNodeScope ){
        if (selectedNodeScope) {
            var parentScope = selectedNodeScope.$parent,
                index,
                targetArray;
            if (parentScope.node) {
                targetArray = parentScope.node.children;
            } else {
                //root node
                targetArray = scope[treeModel];
            }
            // to insert after the index;
            if (targetArray.length) {
                index = targetArray.indexOf(selectedNode);
                console.log('index :: ', index, 'parent::', targetArray);
                if (index !== -1) {
                    //targetArray.splice(index, 0, newNodeToBeAdded)
                }
            }
        }
    }
    // other stuff
}

In Directive template

<span data-ng-class="node.selected" data-ng-click="' + treeId + '.selectNodeLabel(node, this)">{{node.' + nodeLabel + '}}</span>

Working Fiddle