2016-11-19 10 views
0

コントローラのツリーから選択したチェックボックスの値を下の例から得るにはどうすればいいですか?ボタンをクリックすると、すべてのチェックボックス名が配列に表示されます。このplunkerリンクを見てください:ここに私のplnkr- https://plnkr.co/edit/OSpLLl9YrlzqhM7xsYEv?p=preview //コードをここに、angularjsのツリーから選択されたチェックボックスの値を取得

//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); 

答えて

0

こんにちはです。それが働いた

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); 
+0

ここに動作します。それを共有してくれてありがとう。上記のコードでselectedItemsオブジェクトを取得すると、もう1つの質問をしたいと思います。JSONファイルに保存するにはどうすればいいですか? Anyhelpは非常に高く評価されます。 – shaaa

+0

更新されたリンク –

+0

を参照してください。これは私の必要条件ではありません。上記のJSONデータをボタンをクリックしてローカルに保存したい(ex: - items.json)。 – shaaa

関連する問題