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

3

16

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?

Marinelli answered 6/8, 2015 at 21:13 Comment(0)
M
19

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

node = nodes.get(nodeId);
Marinelli answered 7/8, 2015 at 9:25 Comment(1)
The document at visjs.org is no longer valid. It's now at visjs.github.io/vis-data/data/dataset.htmlIngress
M
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);
            }
        });
Mooneye answered 24/5, 2018 at 16:39 Comment(0)
C
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 
}
Crackling answered 29/8, 2018 at 13:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.