0

我有一个带有自定义模型的 goJS 图。当将一个节点放到另一个节点上时,我会在 mouseDrop 触发时链接它们,并在 diagram.model 的链接数据中设置 from 和 to:

mydiagram.model.addLinkData({ from: oldNodeModel.key, to: dragNodeModel.key });

这一切都很好。在我的节点模板中,我有一个自定义模板,它使用删除按钮在节点周围放置一个面板。这个删除按钮只是一个带有点击事件的图像。

现在,当我单击删除图像/按钮时,我想立即删除它及其所有子节点。

我的问题是我找不到孩子。

我有像这样的用户事件findNodesOutOf,它不会产生任何结果,findNodesConnected它会找到父节点和子节点并删除很多 - 这不是我想要的。

知道如何解决这个问题吗?

4

1 回答 1

1

您可以使用 diagram.selection 获取要删除的项目:

var nodeToDelete = mydiagram.selection.iterator.first();

接下来要查找此节点的所有子节点,我推荐一个递归函数,它将执行以下操作:

  1. 取要删除的节点,
  2. 使用 mydiagram.getChildrenNodes(nodeToDelete) 查找所有连接的节点
  3. 遍历 cconnected 节点
  4. 检查每个节点是否是子节点,使用 linkNodeModel 并检查链接是否从当前节点到子节点。
  5. 然后用这个子节点再次调用递归函数
  6. 递归函数将返回一个包含所有子节点的数组

然后你可以删除它们。

您的代码将如下所示:

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();
}

递归函数不断检查每个节点的子节点并将它们添加到数组中:

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;
}
于 2015-02-14T01:34:17.737 回答