Я разобрался с решением. Вам необходимо обновить узел:
Эта часть:
$("#teste").click(function(){
root.children.forEach(collapseLevel);
update(root);
});
Удалить обновление (root) и установить обновление внутри функции:
$("#teste").click(function(){
root.children.forEach(collapseLevel);
});
function collapseLevel(d) {
if (d.depth == 1) {
console.log(d.name + " " + d.depth);
if (d.children) {
console.log(d.children);
console.log(d);
d._children = d.children;
d.children = null;
update(d);
}
};
}
Вы можете найти рабочий код здесь: CodePen