在 vis javascript 库中,如何从节点 ID 获取节点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31865910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 14:29:08  来源:igfitidea点击:

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

javascriptvis.js

提问by srayner

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?

然后在单击节点后的事件处理程序中,我得到我单击的节点的 id。如何从其 id 中获取节点对象?

回答by srayner

I actually found the documentation here; http://visjs.org/docs/data/dataset.html

我实际上在这里找到了文档;http://visjs.org/docs/data/dataset.html

node = nodes.get(nodeId);

回答by mike

I had trouble getting refrence to the node object. nound it in Network.body

我无法引用节点对象。把它放进去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);
            }
        });

回答by Francesco

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 
}