i'm working project has crazy loop expand nodes in d3.js canvas interactive. essentially, want expand children. if object has child, want expand them.
i cut out chunk of code this. there's many loops it's ridiculous. how can reduce simple "find children, preform toggle(); , update();"?
$('.expandall').click(function(e) { e.preventdefault(); length = root.children.length; (var = 0; < length; i++) { toggle(root.children[i]); update(root); if (root.children[i]['children']) { childlength = root.children[i]['children'].length; (var j = 0; j < childlength; j++) { toggle(root.children[i]['children'][j]); update(root); if (root.children[i]['children'][j]['children']) { childlength2 = root.children[i]['children'][j]['children'].length; (var k = 0; k < childlength2; k++) { toggle(root.children[i]['children'][j]['children'][k]); update(root); } } } } } });
sounds case recursion:
$('.expandall').click(function(e) { e.preventdefault(); expandall(root); }); var expandall = function (node) { toggle(node); update(node); // edit: if nodes no children lacking children property if (!node.children) { return; } (var = 0, length = node.children.length; < length; i++) { expandall(node.children[i]); } }; i'm not sure toggle , update means, may able perform single top-level update call after calling expandall(root);.
Comments
Post a Comment