首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何处理mxgraph中的disconnect事件?

如何处理mxgraph中的disconnect事件?
EN

Stack Overflow用户
提问于 2020-02-05 14:42:20
回答 1查看 370关注 0票数 0

我正在尝试添加两个顶点并断开它们的连接,使用的是这个示例https://jgraph.github.io/mxgraph/javascript/examples/portrefs.html

在mxGraph中,有一个监听程序可用于连接event https://jgraph.github.io/mxgraph/docs/js-api/files/handler/mxConnectionHandler-js.html#mxConnectionHandler.mxEvent.CONNECT

代码语言:javascript
复制
graph.connectionHandler.addListener(mxEvent.CONNECT, function(sender, evt)
{
  var edge = evt.getProperty('cell');
  var source = graph.getModel().getTerminal(edge, true);
  var target = graph.getModel().getTerminal(edge, false);

  var style = graph.getCellStyle(edge);
  var sourcePortId = style[mxConstants.STYLE_SOURCE_PORT];
  var targetPortId = style[mxConstants.STYLE_TARGET_PORT];

  mxLog.show();
  mxLog.debug('connect', edge, source.id, target.id, sourcePortId, targetPortId);
});

但当我尝试侦听disconnect事件https://jgraph.github.io/mxgraph/docs/js-api/files/util/mxEvent-js.html#mxEvent.DISCONNECT时,这不起作用。

代码语言:javascript
复制
graph.connectionHandler.addListener(mxEvent.DISCONNECT, function(sender, evt)
{
  var edge = evt.getProperty('cell');
  var source = graph.getModel().getTerminal(edge, true);
  var target = graph.getModel().getTerminal(edge, false);

  var style = graph.getCellStyle(edge);
  var sourcePortId = style[mxConstants.STYLE_SOURCE_PORT];
  var targetPortId = style[mxConstants.STYLE_TARGET_PORT];

  mxLog.show();
  mxLog.debug('connect', edge, source.id, target.id, sourcePortId, targetPortId);
});

graph.addListener(mxEvent.DISCONNECT, function(sender, evt)
{
  var edge = evt.getProperty('cell');
  var source = graph.getModel().getTerminal(edge, true);
  var target = graph.getModel().getTerminal(edge, false);

  var style = graph.getCellStyle(edge);
  var sourcePortId = style[mxConstants.STYLE_SOURCE_PORT];
  var targetPortId = style[mxConstants.STYLE_TARGET_PORT];

  mxLog.show();
  mxLog.debug('connect', edge, source.id, target.id, sourcePortId, targetPortId);
});

在这两种情况下,我都无法侦听disconnect事件。

EN

回答 1

Stack Overflow用户

发布于 2020-02-12 15:58:36

我不知道这是不是明智的做法,但对我来说,它是有效的。我不听断开连接,而是“改变”,然后在连接中发生改变时做出反应……下面是如何实现的:

代码语言:javascript
复制
model.addListener(mx.mxEvent.CHANGE, function(sender, evt)
{
  var changes = evt.getProperty('edit').changes;
  for (var i = 0; i < changes.length; i++)
  {
    // Manage any mxChildChange
    if (changes[i].constructor.name ===  "mxChildChange") {
      // This is a deletion case and this is an edge
      if ((changes[i].index === undefined) && (changes[i].child.edge)) {
        // ... Manage the deletion of the changes[i].child
      }
    }
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60070247

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档