You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JointJS获取含关联链接的所有后继元素方法咨询

获取JointJS元素的后继元素及对应关联链接

你提到的graph.getSuccessors(element)确实默认只返回后继元素节点,不过有两种常用的方法可以同时获取到对应的关联链接,下面给你详细说明:

方法一:利用joint.dia.Graph.ExploreOptions参数扩展返回结果

其实getSuccessors方法支持传入第二个参数作为探索配置,通过设置returnLinks: true,可以让它返回包含节点和链接的混合集合,你可以通过element.isLink()来区分元素类型:

// 获取包含后继元素和对应链接的混合数组
const successorsWithLinks = graph.getSuccessors(element, { returnLinks: true });

// 遍历区分元素和链接
successorsWithLinks.forEach(item => {
  if (item.isLink()) {
    console.log('关联链接:', item);
  } else {
    console.log('后继元素:', item);
  }
});

如果希望得到结构更清晰的分离结果,可以直接使用graph.explore()方法(getSuccessors本质是封装了这个方法),配置direction: 'outgoing'指定探索后继方向,同时开启returnLinks: true,它会返回一个包含nodes和links字段的对象:

const explorationResult = graph.explore(element, {
  direction: 'outgoing', // 指定探索后继(outgoing对应出边方向)
  returnLinks: true
});

console.log('后继元素列表:', explorationResult.nodes);
console.log('对应关联链接列表:', explorationResult.links);

方法二:分步获取元素与对应链接

如果你更习惯分步处理,可以先获取所有后继元素,再通过graph.getConnectedLinks()方法,针对每个后继元素查询与原元素关联的出边链接:

const successors = graph.getSuccessors(element);
const relatedLinks = [];

successors.forEach(successor => {
  // 获取原元素指向当前后继元素的出边链接
  const links = graph.getConnectedLinks(element, {
    outbound: true, // 仅获取从原元素出发的链接
    target: successor // 指定链接目标为当前后继元素
  });
  relatedLinks.push(...links);
});

console.log('后继元素:', successors);
console.log('对应关联链接:', relatedLinks);

这两种方法都能满足你的需求,你可以根据代码风格和业务场景选择合适的方式~

内容的提问来源于stack exchange,提问作者Rafal Cypcer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:39:15