admin管理员组文章数量:1426594
Defining nodes in vis.js, .html , looks like this:
var nodes = [
{id: 1, label: 'Node1'},
{id: 2, label: 'Node2'},
{id: 3, label: 'Node3'},
{id: 4, label: 'Node4'}
];
What I'm trying to find out is, can you create a node double-click (or other event/click type) action, something like so:
var nodes = [
{id: 1, label: 'Node1', double-click: 'Arbitrary_OnClick_Function()'},
{id: 2, label: 'Node2', double-click: 'document.getElementById("div1").innerHTML="Node 2 was clicked"'},
{id: 3, label: 'Node3', double-click: ';target="_blank"'},
{id: 4, label: 'Node4', double-click: '#SomePageSection'}
];
Perhaps there's a way to do this bining with angular.js (github/edgaraafelix/angular-visgraph) or non-simply through vis.js method on(event,callback)
(http//visjs/docs/network.html#Methods
and .html#Events).
Defining nodes in vis.js, http://visjs/network_examples.html , looks like this:
var nodes = [
{id: 1, label: 'Node1'},
{id: 2, label: 'Node2'},
{id: 3, label: 'Node3'},
{id: 4, label: 'Node4'}
];
What I'm trying to find out is, can you create a node double-click (or other event/click type) action, something like so:
var nodes = [
{id: 1, label: 'Node1', double-click: 'Arbitrary_OnClick_Function()'},
{id: 2, label: 'Node2', double-click: 'document.getElementById("div1").innerHTML="Node 2 was clicked"'},
{id: 3, label: 'Node3', double-click: 'https://www.google.;target="_blank"'},
{id: 4, label: 'Node4', double-click: '#SomePageSection'}
];
Perhaps there's a way to do this bining with angular.js (github./edgaraafelix/angular-visgraph) or non-simply through vis.js method on(event,callback)
(http//visjs/docs/network.html#Methods
and http://visjs/docs/network.html#Events).
- Can you maybe accept the answer to mark the question as resolved? This will help other users as well. – MERose Commented Apr 18, 2016 at 11:23
3 Answers
Reset to default 6So from that same doc that you have the nodes example from, you eventually create a network e.g.
var network = new vis.Network(container, data, options);
(the data object contains the nodes and edges)
then you can put an event listener on the network, and you know which node you clicked on from the properties like this
network.on( 'click', function(properties) {
alert('clicked node ' + properties.nodes);
});
I had exactly the same need. Starting with Simon's answer I ended up with adding the following code:
network.on("doubleClick", function (params) {
params.event = "[original event]";
window.open("/myurl/?id="params.nodes[0],'_blank');
});
params.nodes[0]
is the id of the node I'm using in url.
More examples available in source of this visjs page (updated link as per PLG's ment).
Issues: I this works on any network object you double click. I don't know how to make it work on nodes only...
myNetwork.on('doubleClick', function(obj) {
// my crazy code
});
If you listen to the “doubleClick” event, you get an object. I quote from the documentation:
obj = {
nodes: [Array of selected nodeIds],
edges: [Array of selected edgeIds],
event: [Object] original click event,
pointer: {
DOM: {x:pointer_x, y:pointer_y},
canvas: {x:canvas_x, y:canvas_y}
}
}
In the object you get the coordinates of the click in the canvas. You can use this to query the NodeId and get the respective node.
const nodeId = myNetwork.getNodeAt(obj.pointer.canvas.x, obj.pointer.canvas.y); // e.g. 12
Take the id and get the node object
const nodeObj = myNetwork.nodes.get(nodeId)
Now you have access to the object and can do whatever you want with the properties within
本文标签:
版权声明:本文标题:angularjs - Is there a simple way to create a hyperlinkjavascript-action on a node double-click in vis.js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745482794a2660249.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论