15

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?

rene
  • 41,474
  • 78
  • 114
  • 152
srayner
  • 1,799
  • 4
  • 23
  • 39

3 Answers3

19

I actually found the documentation here; https://visjs.github.io/vis-data/data/dataset.html#Getting_Data

node = nodes.get(nodeId);
Programster
  • 12,242
  • 9
  • 49
  • 55
srayner
  • 1,799
  • 4
  • 23
  • 39
  • 1
    The document at visjs.org is no longer valid. It's now at https://visjs.github.io/vis-data/data/dataset.html – Metalskin Sep 12 '20 at 06:49
4

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);
            }
        });
mike
  • 2,149
  • 20
  • 29
3

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 
}
Francesco
  • 1,128
  • 12
  • 22