2

i am using directive concept in angularjs to display selected node value for tree view using click event function in angularjs.below is my sample code

Tree.html:

<div
  data-angular-treeview="true"
  data-tree-model="roleList"
  data-node-id="roleId"
   data-node-label="roleName"
   data-node-children="children"
    data-ng-click="selectNode(roleList[1])"
    data-node-children="children">
    </div>

treeviewcontroller.js:

$scope.roleList1 = [
        { "roleName" : "User", "roleId" : "role1", "children" : [
          { "roleName" : "subUser1", "roleId" : "role11", "children" : [] },
          { "roleName" : "subUser2", "roleId" : "role12", "children" : [
            { "roleName" : "subUser2-1", "roleId" : "role121", "children" : [
              { "roleName" : "subUser2-1-1", "roleId" : "role1211", "children" : [] },
              { "roleName" : "subUser2-1-2", "roleId" : "role1212", "children" : [] }
            ]}
          ]}
        ]},

        { "roleName" : "Admin", "roleId" : "role2", "children" : [] },

        { "roleName" : "Guest", "roleId" : "role3", "children" : [] }



      ];

Treeview.js:

    scope.selectNode = function(val)
    {
    alert(val.roleName);
    }



output:
user
      subuser1
      subuser1-1
 Admin
     subadmin1

from this output in alert place 'Admin' will be dispalyed by click on Admin node.but i want to display dynamically selected node value in click event function.please suggest me how to do this. Thanks

2 Answers 2

3

In order to dynamically select node of the tree I did following:

Let's say you want to select first (top, [0]) element of your tree. so first add data-tree-id="myTreeId" to your HTML:

<div
    data-angular-treeview="true"
    data-tree-model="roleList"
    data-node-id="roleId"
    data-node-label="roleName"
    data-node-children="children"
    data-ng-click="selectNode(roleList[1])"
    data-node-children="children"
    data-tree-id="myTreeId">
</div>

than in Controller: $scope.roleList1[0].selected = "selected"; $scope.myTreeId.currentNode = $scope.roleList1[0];

Sign up to request clarification or add additional context in comments.

Comments

-1

In Tree.html :

Add line : data-tree-id="mytree"

Modify data-ng-click event : data-ng-click="selectNode(mytree.currentNode.roleName)"

In Treeview.js : scope.selectNode = function(val) { alert(val); }

6 Comments

i am doing above changes also but still i am getting undefined value in Treeview.js.
I have created a plunker http://plnkr.co/edit/RuUFemqpouPEFIcBBIXh?p=preview .Please let me know if this what you were looking for
it is working only for using controller but i am using directive then i am getting undefined value in Treeview.js
Can you please provide some plunker
plnkr.co/edit/BVdnb20S6dPwCLsdNioo?p=catalogue so please suggest me how to do this.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.