我正在研究D3树布局,我想从父节点中删除单个节点。我有那个工作。我基本上进入数据结构,抓取父节点的children数组并删除我想删除的节点。然后我称之为“更新”,这几乎是任何人在这些示例中调用的通用更新。
var update = function () {
//At this point the node I removed isn't in the parent's child node array.
//I only remove leaf nodes. I verified that the node isn't in the root variable.
nodes = tree.nodes(root).reverse();
//The nodes array is returned with extra, duplicate nodes.
//They seem to repeat based on what node is deleted.
links = tree.links(nodes);
//Links appear all over the place because of the redundant nodes.
...
我的删除功能如下所示:
//*Only nodes without children can be deleted.
this.removeNode = function (node) {
parent = node.parent;
parent.children.forEach(function(child, i) {
if(node.name === child.name)
{
parent.children.splice(i,1); //remove the node from its parent
}
if(parent.children.length === 0)
{
parent.end = true;
}
});
update();
}
为什么会这样?
答案 0 :(得分:0)
事实证明我在添加子节点之后。我正在使用D3制作一个多树。我意识到我可以使用网络地图,但我发现力布局不稳定且难以驯服。问题是父节点将指向另一个父节点的子节点。这导致了意外的行为。要创建多父树,您必须管理树布局之外的额外链接连接。