paul shannon
paul shannon

Reputation: 375

cytoscape.js select all nodes in an (externally supplied) array

This works nicely:

cy.filter("node[name='KRAS']").select()

How would I do the following? (in pseudo-code, where %in% [if it existed] would test for set membership):

cy.filter("node[name %in% ['KRAS', 'TP53', 'PDGFRA']]).select()

Thanks!

Upvotes: 4

Views: 5066

Answers (1)

maxkfranz
maxkfranz

Reputation: 12242

As you suspect, it's better to batch calls that result in rendering (like eles.select()) -- it batches stylesheet applications and notifications to the renderer to make things faster.

As for your selector, remember that concatenating selectors together is a logical AND and that separating selectors with a comma is a logical OR. So, your result is:

cy.filter('node[foo = "bar"], node[foo = "baz"], node[foo = "bat"]').select();

If the graph is static or changes infrequently, you can also do some caching. By factoring out common parts of each selector, the overall filtering becomes cheaper -- i.e. in your case, if they're all nodes, just filter the set of nodes for each specific selector:

var nodes = cy.filter('node'); // a cached copy of nodes

// each time you want to filter
nodes.filter('[foo = "bar], [foo = "baz"], [foo = "bat"]').select();

Notice how the second filter() call will be faster in this case because it's only operating on a subset of the graph.

Upvotes: 7

Related Questions