Cytoscape JS-每当单击节点时调用一个函数

时间:2019-10-10 12:26:37

标签: cytoscape.js cytoscape-web

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

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'
            }
          }]
      });

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

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

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

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

知道我在做什么错吗?

1 个答案:

答案 0 :(得分:3)

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

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()方法,该方法似乎适用于新添加的节点:)