3

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?

Waldorf
  • 33
  • 1
  • 3
  • Note that I've achieved to replicate the visual effect of adding and removing elements from the graph by removing/adding a CSS class (i.e. using display: 'none'), but nevertheless I'm still very interested in a solution to the outlined issue. Particularly because removing elements using CSS, even when using display: 'none', does not keep the layout algorithms from acting as if a node is present in the graph. – Waldorf Aug 24 '15 at 12:43
  • This solution deletes a node and it's children (nodes plus edges), and can restore them: https://stackoverflow.com/questions/30490567/collapsing-expanding-compound-node-in-cytoscape/67829648#67829648 – Victoria Stuart Jun 03 '21 at 23:19

2 Answers2

5

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.

  • Thanks, at first I disregarded this approach, because it felt like an expensive operation, but I suppose there is no alternative. Depending on its implementation I guess it can still be rather snappy. – Waldorf Aug 26 '15 at 15:25
  • Thanks a bunch for this! I was looking into capturing all information of removed nodes and couldn't seem to find a way to merge two collections together, your answer provided a solution I was searching for way too long. Cheers! – jlos May 28 '19 at 20:13
0

With CytoscapeJS 2.5.4 I can run the following and the connected nodes are removed and restored

removedData = cy.remove("#node2");

--pause--

removedDate.restore();
Craig
  • 8,093
  • 8
  • 42
  • 74