Angular tree checkboxes

I couldn't find a really good suitable directive for creating a checkbox tree view from a JSON structure, so I did it with a self-replacing iterator as shown here:

http://jsfiddle.net/u2ho9d3j/

Now the only problem I am facing is that (see the "Jeans" data on line 40, this is:

"chk": true,

      

This, of course, mark the "Jeans" -number, but not higher. These values ​​will come from the database (where even the aforementioned bransch has chk = true, but nevertheless I'm wondering how the initial bubble can be triggered and all parents marked as marked if the item is marked true.

Can anyone help me understand how this can be done?

Thank you so much!

Christoph

+3


source to share


1 answer


I would preprocess the tree data in a recursive way similar to your setData function:

 function initTree(tree) {
   function processNode(node) {
     angular.forEach(node.children, function(child) {
       if(processNode(child) === true) {
         node.chk = true;   
       }
     });

     return node.chk;
   }

   angular.forEach(tree, processNode);
 };
 initTree($scope.tree);

      



See the updated fiddle http://jsfiddle.net/65yucqge/

Edit Here's another script showing how to get checkbox data into an array: http://jsfiddle.net/tmakin/kmhw1ue0/

+1


source







All Articles