cytoscape.jscytoscape-web

Cytoscape js - Call a function whenever a node is clicked


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());
});

Any idea what am I doing wrong?


Solution

  • 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>

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