我有一个带有自定义模型的goJS图表。当删除另一个节点上的节点时,我会在mouseDrop触发时链接它们,并在图上的链接数据中设置from和to。模式:
mydiagram.model.addLinkData({ from: oldNodeModel.key, to: dragNodeModel.key });
一切正常。在我的节点模板中,我有一个自定义模板,它使用删除按钮在节点周围放置一个面板。此删除按钮只是一个带有点击事件的图像。
现在,当我单击删除图像/按钮时,我想立即删除它及其所有子节点。
我的问题是我找不到孩子。
我有像findNodesOutOf
之类的用户事件,它们没有产生任何结果,findNodesConnected
会找到父节点和子节点并删除该批次 - 这不是我想要的。
知道如何解决这个问题吗?
答案 0 :(得分:1)
您可以使用diagram.selection:
获取要删除的项目var nodeToDelete = mydiagram.selection.iterator.first();
接下来找到这个节点的所有子节点,我推荐一个递归函数,它将执行以下操作:
然后你可以删除它们。
您的代码将如下所示:
function deleteNode()
{
// TAKE NOTE - This will get all selections so you need to handel this
// If you have multiple select enabled
var nodeToDelete = mydiagram.selection.iterator.first();
var childNodes = getChildNodes(deletedItem);
//Remove linked children
$.each(childNodes, function()
{
myDiagram.remove(this);
});
// Then also delete the actual node after the children was deleted
// TAKE NOTE - This will delete all selections so you need to handle this
// If you have multiple select enabled
mydiagram.commandHandler.deleteSelection();
}
递归函数不断检查每个节点的子节点并将它们添加到aray:
function getChildNodes(deleteNode)
{
var children = [];
var allConnected= deleteNode.findNodesConnected();
while (allConnected.next())
{
var child = allConnected.value;
// Check to see if this node is a child:
if (isChildNode(deleteNode, child))
{
// add the current child
children.push(child);
// Now call the recursive function again with the current child
// to get its sub children
var subChildren = getChildrenNodes(child);
// add all the children to the children array
$.each(subChildren, function()
{
children.push(this);
});
}
}
// return the children array
return children;
}
此函数将通过查看图中的链接并检查来回与当前节点和子节点来检查节点是否为子节点:
function isChildNode(currNode, currChild)
{
var links = myDiagram.links.iterator;
while (links.next())
{
// Here simply look at the link to determine the direction by checking the direction against the currNode and the child node. If from is the current node and to the child node
// then you know its a vhild
var currentLinkModel = links.value.data;
if (currentLinkModel.from === currNode.data.key && currentLinkModel.to === currChild.data.key)
{
return true;
}
}
return false;
}