首页 > 解决方案 > 删除网络 vis.js 中的边

问题描述

在 vis.js 中,我可以轻松添加一条边(前功能测试),但我无法删除现有边(前功能测试 1)。为什么 ?

不幸的是,我现在无法访问文档:visjs.org 的“这里没有 GitHub Pages 站点”。谢谢 !

<input onclick=test() type="button" value="add edge 1 to 4 : ok">
<input onclick=test1() type="button" value="remove edge 1 to 2: not ok">

<div id="mynetwork"></div>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/vis/4.21.0/vis.js"></script>

<script type="text/javascript">

    function test(){
        data.edges.add([{from: 1, to: 4}]);
    }

    function test1(){
        data.edges.remove([{from: 1, to: 2}]);
    }

    // GENERAL TEST
    var nodes = [
    {id: 1, label: 'Node 1'},
    {id: 2, label: 'Node 2'},
    {id: 3, label: 'Node 3'},
    {id: 4, label: 'Node 4'}
    ];

    var edges = [
    {from: 1, to: 2},
    {from: 1, to: 3},
    {from: 2, to: 4}
    ];

    nodesd = new vis.DataSet(nodes);
    edgesd = new vis.DataSet(edges);
    var container = document.getElementById('mynetwork');
    var data = {
        nodes: nodesd,
        edges: edgesd
    };

    var options = {
        nodes: {
            size:15
        },
        edges: {
            color: {
                inherit: "both"
            }
        }
    };
    network = new vis.Network(container, data, options);
</script>

标签: vis.js

解决方案


现在好了!

我可以用边缘的“id”来做到这一点:

var edges = [
{id:1, from: 1, to: 2},
{id:2, from: 1, to: 3},
{id:3, from: 2, to: 4},
{id:4, from: 2, to: 5}
];

function test1(){
data.edges.remove([1]);
}

推荐阅读