swimmingdale
swimmingdale

Reputation: 13

layout doesn't work when adding nodes to graph dynamically

When I add nodes to the graph statically via the elements property of

    $('#cy').cytoscape(..) 

the layout option works but when I add them via

    cy.add({..}) 

the layout is ignored. I can apply new layout only on these events(click, mouseover, mouseout) like this:

    cy.on('mouseover', function(event) {
    cy.layout({name: "grid"});          
    });

and the layout changes. Tried with other events: ready, done and load but it doesn't work.

Is there a normal way way to change the layout when elements are added dynamically?

Upvotes: 1

Views: 1580

Answers (1)

maxkfranz
maxkfranz

Reputation: 12242

You can't call a second layout while the initialisation layout is running. Configure your initialisation properly (http://cytoscape.github.io/cytoscape.js/#core/initialisation) to have all the data and options you require.

As for cy.add(): Don't try using cy.add() on load unless you specify everything you need (incl. position) for those elements. Or, you'll have to at least wait for layoutstop before running a new layout. In general, you're better off using the initialisation options to do things for you rather than having to worry about edge cases and event synchronising yourself.

Upvotes: 0

Related Questions