Skip to content
maxkfranz edited this page Mar 8, 2012 · 12 revisions

eles.data()

Read and write client-defined data associated with the elements.

ele.data()

Gets the whole data key-value object for the element

ele.data( key )

Gets the data field with the specified key

key : The name of the data field to get for the element

eles.data( key, value )

Sets the data field with the specified key to the specified value for all elements in the collection

key : The name of the data field to set for the elements
value : The value to set to the data field for the elements

eles.data( obj )

Updates the data fields with those specified in obj for all elements in the collection

obj : The object of key value pairs to update for the elements

Details

When called with no arguments, ele.data() returns the entire data object, a key-value pair object with keys corresponding to data field names for the element.

It is important to note that when setting data this function updates all elements in the collection with the same key-value pairs specified. Getting the data for a collection returns an aligned-index array of the data for each element in the collection.

It is also important to note that some data fields are immutable. You can not change the following data fields:

  • id : id can not be changed for both nodes and edges
  • source : source can not be changed for edges
  • target : target can not be changed for edges

Examples

Get the whole data object:

var data = cy.nodes("#n1").data();

$.each(data, function(key, value){
  console.log("%s = %o", key, value);
});

Get the id data attribute:

var id = cy.nodes("#n1").data("id");
console.log("ID should be `n1`: `$s`", id);

Set weight:

cy.nodes("#n1").data("weight", 56);

Set the foo and bar via an object:

cy.nodes("#n1").data({
  foo: 1,
  bar: 2
});

Clone this wiki locally