Mohit Grover
Mohit Grover

Reputation: 17

How to implement the functionality in cytoscape.js to add an edge between two nodes by clicking the nodes?

I want to connect two nodes when the user clicks one node first and then the second node.

Upvotes: -1

Views: 361

Answers (1)

Stephan T.
Stephan T.

Reputation: 6074

Try using the cy.bind() function. You can bind the click event on a node and save the id of this node in an array for example:

cy.unbind('click');          // unbind event to prevent the event to fire 

cy.bind('click ', 'node', function (evt) { 
    // add the node to an array (node is in evt.target())
    // if there are 2 nodes in the array, remove them after creating an edge between them
    cy.add(//your new edge);
    array.splice(0, 2);   // remove the first 2 elements of your array
});

Upvotes: 0

Related Questions