user1768233
user1768233

Reputation: 1461

How can I group nodes for seperate on click events in cytoscape.js

I want to 'tag' nodes in cytoscape.js so I can have different on click handlers for different groups of nodes. I understand I can use their id or name as a selector however I'm wondering if I can use a custom tag to do this?

Here is what I was trying..

    <style>
        body {
            font-family: helvetica;
            font-size: 14px;
        }

        #cy {
            width: 100%;
            height: 100%;
            position: absolute;
            left: 0;
            top: 0;
            z-index: 999;
        }

        h1 {
            opacity: 0.5;
            font-size: 1em;
        }
    </style>

    <script>
        $(function(){

            var cy = window.cy = cytoscape({
                container: document.getElementById('cy'),
                                    textureOnViewport: true,
                                    pixelRatio: 1,

                ready: function(){
                },

                style: [
                    {
                        selector: 'node',
                        css: {
                            'content': 'data(name)',
                            'shape':'data(shape)',
                            'width': 'data(width)',
                            'height': 'data(height)',
                            'border-color': 'data(bordercolor)',
                            'border-width': 'data(borderwidth)',
                            'color': '#fff',
                            'type': 'cat'
                    }
                    },
                    {
                        selector: 'edge',
                        css: {
                            'target-arrow-shape': 'triangle',
                            'content': 'data(label)'
                        }
                    },
                ],


            });




        });

                    $.getJSON('<%= url_for(‘getdata’) %>',
                        function (data) {
                              cy = $('#cy').cytoscape("get");
                              cy.load(data);
                              cy.layout({name: 'cose',           
                                        });

                              cy.$('node[type = “cat”]’).on('click',function(e){
                             //do something  
                                                                  });

                        });



    </script>
</head>

<body>
    <h1>Demo</h1>

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

</body>

Upvotes: 0

Views: 998

Answers (1)

maxkfranz
maxkfranz

Reputation: 12250

You've misplaced element data and put it in the stylesheet. Refer to the JSON spec: http://js.cytoscape.org/#notation/elements-json

Upvotes: 1

Related Questions