javascript 如何删除jsPlumb连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11488067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete jsPlumb connection
提问by GigaPr
回答by Rafik Bari
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.
出于某种原因,分离对我不起作用。上面的方法在他们的文档中没有提到,可能他们忘了更正他们的文档。
回答by philsch
Update:jsPlumb no longer implements the
detach
method. Please see Rafik Bari's answerbelow for a more up-to-date solution.
更新:jsPlumb 不再实现该
detach
方法。请参阅下面的Rafik Bari 的回答以获取最新的解决方案。
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);
});
回答by GigaPr
I found the solution in the documentation (http://jsplumb.org/doc/usage.html)
我在文档中找到了解决方案(http://jsplumb.org/doc/usage.html)
jsPlumb.detachAllConnections("elementId");
回答by user1087079
If source div or target div id is already known, then you can delete the connection like this:
如果源 div 或目标 div id 已知,那么您可以像这样删除连接:
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;
您还可以使用 jsPlumb.getAllConnections() 获取所有连接的数组,并测试每个连接的 sourceId 或 targetId 以准确获取您需要的连接;
回答by Nayantara Jeyaraj
After all the endpoint connections, to and from the element are deleted, you need to detach it as shown in the 3rd line
删除所有端点连接后,进出元素,您需要将其分离,如第 3 行所示
jsPlumb.detachAllConnections(divID);
jsPlumb.removeAllEndpoints(divID);
jsPlumb.detach(divID);
divID.remove()
回答by Arivanandan
回答by Kuba Urbańczyk
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);
}
}
});
回答by AllenC
Hi you can refer to this: Detach connection jsPlumb
嗨你可以参考这个: Detach connection jsPlumb
This block of code allows to detach connection if a connection was clicked:
如果单击连接,则此代码块允许断开连接:
jsPlumb.bind("click", function(conn) {
jsPlumb.detach(conn);
});
回答by Eisus
I want to update the answer. For version 2.5, if you use
我想更新答案。对于 2.5 版,如果您使用
jsPlumb.detach()
you may get error message that: jsPlumb.detach() is not a function. And I also try
您可能会收到错误消息:jsPlumb.detach() 不是函数。我也尝试
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
连接确实被删除了,但是,我不确定为什么端点只是从 div 分离。最后通过以下方式解决
instance.deleteConnection(conn)
回答by filippos lavdas
Firtsly, you can find your connection by using sourceId and targetId
首先,您可以使用 sourceId 和 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.
这将返回一组 jsPlumb 连接,因此您可以删除与此源和目标 ID 相关的所有连接。
for(var i=0;i < connections.length;i++) {
jsPlumb.deleteConnection(connections[i]);
}