Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I'm building a nested tree and using AngularJS input type checkboxes in all the leaves. Most of the examples I've seen use ng-repeat over a single list, bind the model to the list item, and then get the selected values.

But the items in my tree are all in different lists so I'm not sure how to bind them to the same model. What other ways are there or what solution is there to getting all the checked items without them being in the same model?

An example of the data I'm using, a leaf would be a node without any children:

[{
'Name': 'A',
'Children': [{
    'Name': 'AA',
    'Children': []},
    'Name': 'AB',
    'Children': [{
        'Name': 'ABA'
        'Children': []
    }],
'Name': 'B',
'Children': [{
    'Name': 'BA',
    'Children': [{
        'Name': 'BAA',
        'Children': []
        }]
    }]
}]

I'm using Angular UI Tree for my tree and the basic markup looks something like this:

<script type="text/ng-template" id="tree.html">
  <div ui-tree-handle class="tree-node tree-node-content">
      <input data-ng-show="{{node.Children.length == 0}}"
              type="checkbox"
              data-ng-model="node.selected">
      <span class="tree-node">
              {{node.Name}}
      </span>
  </div>
  <ol ui-tree-nodes data-ng-model="node.Children">
      <li ng-repeat="node in node.Children"
          ui-tree-node
          ng-include="'tree.html'">
      </li>
  </ol>
</script>

<div ui-tree=""
    id="tree-root">
    <ol ui-tree-nodes ng-model="treeList">
        <li ng-repeat="node in treeList"
            ui-tree-node
            ng-include="'tree.html'"></li>
    </ol>
</div>

In this example I'm binding the checkbox to a node.selected attribute in the node. But then to find all the selected nodes I would have to traverse my entire tree and check if each node is selected. Since my tree is quite big this would be unfeasible, is there another way to do it?

share|improve this question
1  
it would be great if you post some examples; so, we can better assist you. For now, I think you just need to understand how scope hierarchy works. Once you understand the concept, you will see that is easy to achieve what you want: github.com/angular/angular.js/wiki/Understanding-Scopes – Rafa May 27 at 19:39
    
Yeah I've been using angular for a few months now and still don't really get scope. Posting some examples now. – Niel May 28 at 9:19
    
I am super busy today and won't be able to help (maybe Saturday) but in the mean while. How about if you merge the data in a single list so you can use the examples you are familiar with? Clearly, this approach won't work if you need databind values from these different list... – Rafa May 28 at 13:45
    
You can create a $watch function to listen any changes in your data (node data). And add selected check box to a new array and vice versa. – Abhilash P A Sep 24 at 9:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.