[英]why Visjs network is reappearing when i try to update any node
I am trying to update the hidden attribute of visjs network's node.我正在尝试更新 visjs 网络节点的隐藏属性。 by clicking the node, the node should be invisible without disturbing the graph.通过单击节点,该节点应该是不可见的,而不会干扰图形。 can anyone help me with this I am new.谁能帮我这个我是新来的。 code I am trying -我正在尝试的代码 -
this.network.on('click', function (properties) {
var ids = properties.nodes;
if(ids.length==0)
return
treeData.nodes.update({id:ids,hidden:true}) //treeData is object containing two dataSets (nodes and edges);
});
The object generated by the click event contains an array of IDs rather than a single ID.单击事件生成的对象包含一组 ID 而不是单个 ID。 The definition of this can be seen at https://visjs.github.io/vis-network/docs/network/#Events .这个定义可以在https://visjs.github.io/vis-network/docs/network/#Events看到。
At present the code is passing this array in as a single id treeData.nodes.update({id:ids,hidden:true})
which generates an error "Error: Node must have an id"
.目前,代码将此数组作为单个 id treeData.nodes.update({id:ids,hidden:true})
,这会生成错误"Error: Node must have an id"
。 Instead of this it should loop through the array of IDs updating each.相反,它应该遍历更新每个 ID 的数组。
network.on('click', function (properties) {
var ids = properties.nodes;
// ids is an array, update each item
for (let i = 0; i < ids.length; i++){
treeData.nodes.update({id:ids[i],hidden:true})
}
});
Example can be found at https://jsfiddle.net/3yshgam1/ and also incorporated into this post below.示例可以在https://jsfiddle.net/3yshgam1/上找到,也包含在下面的这篇文章中。
// create an array with nodes var nodes = new vis.DataSet([ { id: 1, label: "Node 1" }, { id: 2, label: "Node 2" }, { id: 3, label: "Node 3" }, { id: 4, label: "Node 4" }, { id: 5, label: "Node 5" }, ]); // create an array with edges var edges = new vis.DataSet([ { from: 1, to: 3 }, { from: 1, to: 2 }, { from: 2, to: 4 }, { from: 2, to: 5 }, { from: 3, to: 3 }, ]); // create a network var container = document.getElementById("mynetwork"); var treeData = { nodes: nodes, edges: edges, }; var options = {}; var network = new vis.Network(container, treeData, options); network.on('click', function (properties) { var ids = properties.nodes; // ids is an array, loop through the items for (let i = 0; i < ids.length; i++){ treeData.nodes.update({id:ids[i],hidden:true}) } });
#mynetwork { width: 600px; height: 180px; border: 1px solid lightgray; }
<script src="https://visjs.github.io/vis-network/standalone/umd/vis-network.min.js"></script> <div id="mynetwork"></div>
Full example:完整示例:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.