Skip to content
Advertisement

How can I inject a variable in selectors in Cytoscape JS?

I am trying to visualize edges of a graph with different widths by selecting edges based on their id fields. Is it possible to use variables in selector queries? How can I achieve this behaviour? There are ways to work around this by repeating code, for example:

if (i==0){  
        edge_item = cy.elements('edge[id = "edge_0"]');

        cy.style()
          .selector(edge_item)
          .style({
          'width': 10
          })
          .update();
      }

I would however prefer a cleaner solution, preferably by using a variable instead of “edge_0” above with something like the following:

edge_var = "edge_" + i;
edge_item = cy.elements('edge[id = "edge_var"]');

Is this possible?

Advertisement

Answer

Apparently, this is done by escaped characters.

var nodeId = "edge_";
  for (var i = 0; i < 60; i++) {
  cy.remove('edge[id='' + nodeId.concat(i.toString()) + '']');
  }

I came across the answer to this post here in a similar setting.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement