OK, if anyone's interested, I finally came up with a workign solution
(while some what funky... after 3 tries, best I could think of)
//The following is all done outside of "update()"....
var levels = getDepth(data, 0); //used to re-fold the children
data = unfoldChildren(data); //unfolding for first render, then
refolding immediately after to fix bug with rows out of order
function getDepth(obj, level) { var depths;
if (obj.children) { depths =
obj.children.map(function(elem) { return getDepth(elem, level
+ 1); }); } else { depths = [level]; }
return d3.max(depths); }
function unfoldChildren(obj) { if (obj._children) {
obj.children = obj._children; delete obj._children; }
if (obj.children) { obj.children.forEach(function(elem) {
elem = unfoldChildren(elem); }); } return obj; //
technically this mutates the input anyway.... }
function foldChildren(obj, d) { if (obj.children) {
obj.children.forEach(function(elem) { elem =
foldChildren(elem, d - 1); }); if (d <= 0) {
obj._children = obj.children; delete obj.children; }
} return obj; //technically this mutates the input
anyway.... }
var first = true;
update();
function update() {
//...... VERY similar to the second example posted by Mike, BUT in
my case rendering a html table, not svg
//instead of removing, need to just hide, otherwise rows are
appending to END of table, not appending to where they should be...
not an issue in SVG because every node is translated to the correct
position
node.exit()
.style('display', 'none')
.classed('hidden', true)
.classed('highlight', false);
//AFTER the unfolded tree renders, re-fold the data, then re-
render imediately
if (first) {
first = false;
data = foldChildren(data, levels);
update();
}
}
Definitely interested if anyone has a better solution.. BUT at least
this works