srayner
srayner

Reputation: 1837

In the vis javascript library, how do I get the node from its node Id?

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

Answers (3)

srayner
srayner

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

Francesco
Francesco

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

mike
mike

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

Related Questions