-
Notifications
You must be signed in to change notification settings - Fork 0
Collection data
Read and write client-defined data associated with the elements.
Gets the whole data key-value object for the element
Gets the data field with the specified key
key : The name of the data field to get for the element
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 elementsUpdates the data fields with those specified in
objfor all elements in the collectionobj : The object of key value pairs to update for the elements
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:idcan not be changed for both nodes and edges -
source:sourcecan not be changed for edges -
target:targetcan not be changed for edges
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
});