For others in search of a solution to the issue of removing a node and it's children in Cytoscape.js, I tried and failed with the (admittedly dated) solution in the accepted answer: .descendants().
While awaiting a response on GitHub,
https://github.com/cytoscape/cytoscape.js/issues/2877
I devised the following solution. Briefly, I :
- use
.successors() rather than .dependents() (suggested above)
- save the data in a variable; then, restore those data
- obviate the need for
.addClass()
var myNode = cy.elements('node[name="Technology"]');
// cy.collection() : return a new, empty collection
// https://js.cytoscape.org/#cy.collection
var myCollection = cy.collection();
setTimeout(function(){
console.log('Deleting "Technology" node + descendants ...')
// Save data for later recall:
// https://js.cytoscape.org/#cy.remove
myCollection = myCollection.union(myNode.successors().targets().remove());
myNode.successors().targets().remove();
// nested setTimeout():
setTimeout(function(){
console.log('Restoring "Technology" node + descendants ...')
myCollection.restore();
console.log('Done!')
}, 2000);
}, 2000);
