0

I would like to know how can we get the selected checkbox values from tree in controller from the below example? On click of a button i want to display all the checkbox names in an array. Here is my plnkr- https://plnkr.co/edit/OSpLLl9YrlzqhM7xsYEv?p=preview //code goes here,

    //Controller
    Controller to display the tree.
    (function (ng) {
        var app = ng.module('tree', ['tree.service', 'tree.directives']);
        app.controller("TreeController", ["TreeService", function (TreeService) {
            var tc = this;
            buildTree();
            function buildTree() {
                TreeService.getTree().then(function (result) {
                    tc.tree = result.data;
                }, function (result) {
                    alert("Tree no available, Error: " + result);
                });
            }
        }]);
    })(angular);




    //Tree Directive
Directive used for creating tree node.
(function (ng) {
    var app = ng.module('tree.directives', []);
    app.directive('nodeTree', function () {
        return {
            template: '<node ng-repeat="node in tree"></node>',
            replace: true,
            restrict: 'E',
            scope: {
                tree: '=children'
            }
        };
    });
    app.directive('node', function ($compile) {
        return {
            restrict: 'E',
            replace: true,
            templateUrl: 'node.html', // HTML for a single node.
            link: function (scope, element) {
                /*
                 * Here we are checking that if current node has children then compiling/rendering children.
                 * */
                if (scope.node && scope.node.children && scope.node.children.length > 0) {
                    scope.node.childrenVisibility = true;
                    var childNode = $compile('<ul class="tree" ng-if="!node.childrenVisibility"><node-tree children="node.children"></node-tree></ul>')(scope);
                    element.append(childNode);
                } else {
                    scope.node.childrenVisibility = false;
                }
            },
            controller: ["$scope", function ($scope) {
                // This function is for just toggle the visibility of children
                $scope.toggleVisibility = function (node) {
                    if (node.children) {
                        node.childrenVisibility = !node.childrenVisibility;
                    }
                };
                // Here We are marking check/un-check all the nodes.
                $scope.checkNode = function (node) {
                    node.checked = !node.checked;
                    function checkChildren(c) {
                        angular.forEach(c.children, function (c) {
                            c.checked = node.checked;
                            checkChildren(c);
                        });
                    }

                    checkChildren(node);
                };
            }]
        };
    });
})(angular);

1 Answer 1

0

Hello: Look at this plunker link. It works here

https://plnkr.co/edit/vaoCzUJZBf31wtLNJ5f5?p=preview

(function (ng) {
var app = ng.module('tree', ['tree.service', 'tree.directives']);
app.controller("TreeController", ["TreeService", "$scope", function (TreeService, $scope) {
    var tc = this;
    buildTree();
    function buildTree() {
        TreeService.getTree().then(function (result) {
            tc.tree = result.data;
        }, function (result) {
            alert("Tree no available, Error: " + result);
        });
    }


   $scope.selectedItems = [];

   $scope.getSelected = function(){
     $scope.selectedItems = [];
     function checkChildren(c) {
          angular.forEach(c.children, function (c) {
             if (c.checked){
                $scope.selectedItems.push({"selected":c.name});
             }
              checkChildren(c);
          });
     }


      angular.forEach(tc.tree, function(value, key) {
          if (value.checked){
            $scope.selectedItems.push({"selected":value.name});
          }

           checkChildren(value);
      });
   };
}]);})(angular);
Sign up to request clarification or add additional context in comments.

2 Comments

It worked. Thanks for sharing it. I would like to ask one more question, once i got selectedItems object in the above code how can i save it to JSON file. Anyhelp would be much appreciated.
that is not my requirement. I want to save the above JSON data locally (ex:- items.json) on click of a button.

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.