Reputation: 1837
So I create the nodes like this...
var nodes = new vis.DataSet([
{id: 1, label: 'Peter'},
{id: 2, label: 'John'},
{id: 3, label: 'Sally'},
]);
then later on in an event handler after clicking on a node I get the id of the node i clicked. How do I get node object from its id?
Upvotes: 16
Views: 17204
Reputation: 1837
I actually found the documentation here; https://visjs.github.io/vis-data/data/dataset.html#Getting_Data
node = nodes.get(nodeId);
Upvotes: 19
Reputation: 1148
I'm using my own function to get all node objects, but you need to make the 'network' variable as global. For example:
function getNode(nodeId){
var nodeObj= network.body.data.nodes._data[nodeId];
return nodeObj; //nodeObj.label to get label
}
Upvotes: 3
Reputation: 2308
I had trouble getting refrence to the node object. nound it in Network.body
network.on('click', function (properties) {
var nodeID = properties.nodes[0];
if (nodeID) {
var clickedNode = this.body.nodes[nodeID];
console.log('clicked node:', clickedNode.options.label);
console.log('pointer', properties.pointer);
}
});
Upvotes: 4