简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM