Search code examples
javascriptcytoscape.jscytoscape

undo remove nodes operation in Cytoscape JS


When you remove nodes in cytoscape JS (using cy.remove()), the edges attached to these nodes are also deleted from the graph. The description for cy.remove() says it removes elements from the graph and returns them. However, the data that is returned does not include the deleted edges.

As a consequence the following sequence of operations

removedData = cy.remove(someNodes); cy.add(removedData);

do modify the graph, as they could cause some edges to disappear.

How should you perform a reversible removal operation in Cytoscape JS?


Solution

  • You could just include the edges explicitly:

    removedData = cy.remove(someNodes.union(someNodes.connectedEdges()));
    

    Then both removedData.restore() and cy.add(removedData) will restore both nodes and edges.