How to delete jsPlumb connection
Asked Answered
P

11

12

I am playing with jsplumb, but I am not able to delete the connection between two divs having only the id of one div.

Phinney answered 14/7, 2012 at 23:7 Comment(1)
The link now leads to a linkfarm.Enjoy
M
10

To delete a connection use the following code:

jsPlumb.deleteConnection(con)

For some reason, detach did not work for me. The method above is not mentioned in their docs, probably they forgot to correct their docs.

Marillin answered 31/1, 2018 at 1:59 Comment(1)
Hope this helps someone - If you delete a connection between two elements, then you delete the two elements, and then you create two new elements with the same IDs and try to connect them, jsplumb will get confused due to an internal cache it keeps. You must call .reset() on your jsPlumb instance.Vuong
M
8

Update: jsPlumb no longer implements the detach method. Please see Rafik Bari's answer below for a more up-to-date solution.

If you've multiple connections from or to elements, detaching all connections is maybe not the best solution to delete a connection. There is a (not well documented) function to detach exactly one connection:

jsPlumb.detach(connection, {
    fireEvent: false, //fire a connection detached event?
    forceDetach: false //override any beforeDetach listeners
})

In my example, I want to delete a connection when the connector is clicked:

 jsPlumb.bind('click', function (connection, e) {
     jsPlumb.detach(connection);
 });
Matchmaker answered 13/12, 2012 at 10:31 Comment(1)
This answer is outdated, jsPlumb.detach() doesn't exist in the API anymore, use jsPlumb.deleteConnection() instead (see Rafik Bari's response)Plascencia
P
7

I found the solution in the documentation (http://jsplumb.org/doc/usage.html)

jsPlumb.detachAllConnections("elementId");
Phinney answered 15/7, 2012 at 17:58 Comment(0)
T
4

If source div or target div id is already known, then you can delete the connection like this:

var conn = jsPlumb.getConnections({
              //only one of source and target is needed, better if both setted
              source: sourceId,
              target: targetId
            });
if (conn[0]) {
  jsPlumb.detach(conn[0]);
}

You can also use jsPlumb.getAllConnections() to get the array of all connections, and test every connection's sourceId or targetId to get exactly the connection you need;

Tedie answered 3/4, 2014 at 5:57 Comment(0)
G
2

After all the endpoint connections, to and from the element are deleted, you need to detach it as shown in the 3rd line

jsPlumb.detachAllConnections(divID);
jsPlumb.removeAllEndpoints(divID);
jsPlumb.detach(divID); 
divID.remove()
Goldsberry answered 30/5, 2016 at 4:53 Comment(2)
Worked for me. But I used an instance of jsPlumb instead of calling it directly: g_instance = jsPlumb.getInstance(...., Container: "flowchart-demo") and then: g_instance.detachAllConnections(divID);Chapatti
Detaching the connections leaves all Endpoints in place without disorienting the rest of the graph by considering its relativity. So as long as it is performed, the way in which the instances are taken wouldn't really affect the rest of the code.Goldsberry
A
1
jsPlumb.deleteConnectionsForElement(elementId)

works. Though it's old I'd like to add that finding out what methods a library has, provided it's self descriptive enough is quite easy.

dev tools

Annamarieannamese answered 6/6, 2017 at 6:44 Comment(0)
P
1

Firtsly, you can find your connection by using sourceId and targetId

var connections = jsPlumb.getConnections({
            source: sourceId ,
            target:targetId  
        });

This will return an array of jsPlumb connections, so you can delete all connections related to this source and target id.

for(var i=0;i < connections.length;i++) {
            jsPlumb.deleteConnection(connections[i]);
        }
Paratuberculosis answered 10/5, 2018 at 10:22 Comment(0)
L
0

This code removes all "wrong" connections in a game I'm developing. I hope it helps

var wrong_connections = new Array(DB_ID, ANOTHER_DB_ID);
var connections = jsPlumb.getConnections();
$.each(connections, function(index, value) { // going through all connections
  var source_id = value.source.attr('id').substring(28);  // I need the ID from DB so I`m getting it from the source element
  for (var a = 0; a < wrong_connections.length; a++) {  
    if (source_id == wrong_connections[a]) {
      jsPlumb.detach(value);
    }
  }
});
Lemley answered 15/3, 2013 at 16:56 Comment(0)
M
0

Hi you can refer to this: Detach connection jsPlumb

This block of code allows to detach connection if a connection was clicked:

jsPlumb.bind("click", function(conn) {
    jsPlumb.detach(conn);
});
Mckeon answered 17/9, 2013 at 9:30 Comment(0)
U
0

I want to update the answer. For version 2.5, if you use

jsPlumb.detach()

you may get error message that: jsPlumb.detach() is not a function. And I also try

jsPlumb.deleteConnection(conn)

the connection is deleted indeed, however, I'm not sure why the endpoint just detach from the div. Finally, it is solved by

instance.deleteConnection(conn)
Udine answered 4/5, 2018 at 2:32 Comment(0)
R
0

To delete connection worked for me

jsPlumb.bind("click", function(conn) {
    // alert(JSON.stringify(data));
    jsPlumb.deleteConnection(conn);
 });
Respectively answered 3/8, 2018 at 8:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.