I am trying to create a collapsible tree structure in Cytoscape using breadthfirst layout, to replicate the D3 collapsible tree.
I am trying to replicate this type of click action on nodes, but adding restore functionality in addition - Images & breadthfirst layout
The reason I chose Cytoscape is because I had a scenario where the tree would have nodes with more than 1 parent.
I have tried to add a tap event using the following code:
cy.on('tap', 'node', function() {
if (this.scratch().restData == null) {
// Save node data and remove
this.scratch({
restData: this.connectedEdges().targets().remove()
});
} else {
// Restore the removed nodes from saved data
this.scratch().restData.restore();
this.scratch({
restData: null
});
}
}
But, this is successful only to collapse and expand its immediate child nodes (rest of the nodes are still visible) and also causes problem when I tap on leaf nodes.
If anyone knows a way to expand and collapse a node, please help.
Edit: Guys, if anyone knows the solution for a simple multilevel tree also, that would also be a good start...
I replaced this line of code:
with this one:
and this code now collapses children and grandchildren nodes (only tested on 3 levels) and leaf nodes no longer collapse into their parent when clicked.