首页 > 解决方案 > 试图将节点扩展为空父节点

问题描述

我正在使用cytoscape.jscytoscape.js-expand-collapse在我的图形结构中创建动态层次结构。我希望能够动态创建一个折叠(合并)节点,该节点可能会扩展、删除或可能与其他节点重新合并。我每次打电话都有麻烦nodes.move({parent:null})。它们与图表分离,我无法将它们重新附加到新的父节点。如果我打电话restore给他们,我会看到错误,说他们已经存在于图表中。

merge并且unmerge本身在没有现有折叠节点的简单情况下工作正常。然而,对已经包含复合折叠节点的东西调用合并会破坏事情。我将得到没有先前折叠的合并候选者的子节点的结果合并节点

更新 #1我意识到我的问题是返回错误的移动节点。对集合调用 .move 会返回一组新节点。所以unmerge应该返回那些。

更新#2 cytoscape-expand-collapse 实用程序在父节点折叠/展开期间通过在元边缘上的“originalEnds”数据道具中隐藏节点数据来进行一些内部簿记。因此,如果我现在通过将节点移入/移出父节点来动态更改图形,这些 originalEnds 指针会不同步,从而导致无限循环。我正在做一些我自己的额外节点跟踪作为一种解决方法。

function merge(nodes){
    //if one of the nodes is a compound itself, first uncollapse, then merge
    var collapsed = nodes.filter(function(c){return typeof c.data('collapsedChildren')!=='undefined';});
    if (collapsed.length>0) {
        // for now just assume collapsed is a length of 1
        var unmerged = unmerge(collapsed); //unmerged should be now the former collapsed children
        nodes = nodes.subtract(collapsed).union(unmerged.nodes());
    }
    var parentNode = cy.add({group:'nodes', data: {id: parentID}});
    nodes.move({parent: parentID});
    collapseApi.collapse(parentNode);
    return parentNode;
}

function unmerge(parentNode){
    collapseApi.expand(parentNode);
    var children = parentNode.children();
    var moved = children.move({parent:null});
    //at this point children become "detached" from the graph
    // .removed() returns true, however, calling restore() logs an error and they are still technically in the graph
    parentNode.remove();
    return moved;
}

标签: cytoscape.js

解决方案


ele.move()在 cytoscape>=3.4 中有更方便的实现:元素被就地修改,而不是创建替换元素。

使用返回的集合的旧代码ele.move()仍然可以工作。完全不必使用返回的集合可以简化新代码。


推荐阅读