Cytoscape js-每当单击节点时调用一个函数 [英] Cytoscape js - Call a function whenever a node is clicked

查看:55
本文介绍了Cytoscape js-每当单击节点时调用一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我像这样初始化细胞间隙:

I initialized the cytoscape like this:

var cy = cytoscape({
        container: document.getElementById('cy'),
        elements: [
          { data: { id: 'a' } },
          { data: { id: 'b' } },
          { data: { id: 'c' } },
          {
            data: {
              id: 'ab',
              source: 'a',
              target: 'b'
            }
          },
          {
            data: {
              id: 'ac',
              source: 'a',
              target: 'c'
            }
          }]
      });

然后,我添加了一个函数,该函数可以在用户双击视口时添加一个新节点.

Then I added a function which adds a new node whenever the user double clicks on the viewport.

var nid = 1;
document.getElementById("cy").ondblclick = function(e) {
        cy.add({ data: { id: nid }, renderedPosition: { x: e.x, y: e.y } });
        nid++;
        };

然后,我编写了此函数,每当用户单击节点时都应调用该函数.每当用户单击初始化细胞景观时我手动添加的节点时,它都可以工作,但是问题在于它不适用于用户通过双击添加的节点.

Then I wrote this function which should be called whenever user clicks a node. It works whenever user clicks on a node which I added manually when initializing the cytoscape but the problem is its not working for the nodes which user added by double clicking.

cy.$('node').on('click', function (e) {
          console.log('node clicked: ', e.target.id());
        });

知道我在做什么错吗?

推荐答案

我在这里有您的代码的有效版本:

I have a working version of your code here:

var cy = (window.cy = cytoscape({
  container: document.getElementById("cy"),
  boxSelectionEnabled: false,
  autounselectify: true,

  style: [{
      selector: "node",
      css: {
        "label": "data(id)",
        "text-valign": "center",
        "text-halign": "center",
        "background-color": "data(faveColor)"
      }
    },
    {
      selector: "edge",
      css: {
        "curve-style": "bezier",
        "control-point-step-size": 40,
        "target-arrow-shape": "triangle"
      }
    }
  ],
  elements: {
    nodes: [{
        data: {
          id: "a",
          faveColor: "#2763c4"
        }
      },
      {
        data: {
          id: "b",
          faveColor: "#37a32d"
        }
      },
      {
        data: {
          id: "c",
          faveColor: "#37a32d"
        }
      }
    ],
    edges: [{
        data: {
          source: "a",
          target: "b"
        }
      },
      {
        data: {
          source: "a",
          target: "c"
        }
      }
    ]
  },
  layout: {
    name: "dagre"
  }
}));

cy.ready(function() {
  cy.dblclick();
});

var nid = 0;
cy.bind('dblclick', function(evt) {
  cy.add({
    group: 'nodes',
    data: {
      id: nid,
      faveColor: 'red'
    },
    position: {
      x: evt.x,
      y: evt.y
    }
  });
  nid++;
});

cy.bind('click', 'node', function(evt) {
  console.log('node clicked: ', evt.target.id());
});

body {
  font: 14px helvetica neue, helvetica, arial, sans-serif;
}

#cy {
  height: 100%;
  width: 100%;
  float: right;
  position: absolute;
}

<html>

<head>
  <meta charset=utf-8 />
  <meta name="viewport" content="user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, minimal-ui">
  <script src="https://unpkg.com/cytoscape@3.3.0/dist/cytoscape.min.js">
  </script>
  <!-- cyposcape dagre -->
  <script src="https://unpkg.com/dagre@0.7.4/dist/dagre.js"></script>
  <script src="https://cdn.rawgit.com/cytoscape/cytoscape.js-dagre/1.5.0/cytoscape-dagre.js"></script>
  <script src="https://unpkg.com/cytoscape-dblclick/dist/index.js"></script>
</head>

<body>
  <div id="cy"></div>
</body>

</html>

我使用了cy.on()/cy.bind()方法,该方法似乎可以与新添加的节点一起使用:)

I used the cy.on()/cy.bind() method, that seems to work with newly added nodes :)

这篇关于Cytoscape js-每当单击节点时调用一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆