jqueryvis.jsvis.js-network

How to clear/reset/refresh vis.js function for selectNode or selectEdge?


  1. Firstly please refer to this issue (Has been solved).
  2. Continuous from that issue, I want to use selectNode or selectEdge function at same page and same JS function, however the function is unable to click, why this happen? If I don't use previous function, the selectNode or selectEdge is work as well.

So here, anyone know how to clear/delete/refresh previous function prior to clicking the selectNode or selectEdgefunction?

Here is the selectNode or selectEdge function.

function node(){
    // Previous solved issue
    $("#btnSearchNode").on('click',function () {
        for (var i = 0;i<test_array_node.length;i++){
            if (test_array_node[i].label.indexOf($("#inputSearchNode").val()) >=0 && $("#inputSearchNode").val() != ''){
                test_array_node[i].font = {
                    color:'#FFFFFF',
                    background: '#4A77E0',
                    size: 20
                };
            }else{
                delete test_array_node[i].font;
            }
        }
        new vis.Network(container, data, options);
    });
    
    // Current issue - how to clear above function to be able below function can be click
    network.on('selectNode', function (properties){
        console.log('Node is selected');
    },
    network.on('selectEdge', function (properties){
        console.log('Edge is selected');
    }
}

Solution

  • You have written code for selectEdge & selectEdge on page load . So , it has reference of intial network instance . But , when you filter your nodes you are reinitializing i.e : new vis.Network(container, data, options) so previous instance doesn't work . Instead save your instance in some variable and then reinitialize your events .

    Demo Code :

    var allNodes = [{
        id: 1,
        label: "55"
      },
      {
        id: 2,
        label: "192.12"
      },
      {
        id: 3,
        label: "192.16"
      },
      {
        id: 4,
        label: "192.168.1.8"
      },
    ];
    // Create edge data array
    var allEdges = [{
        id: "1_2",
        from: 1,
        to: 2
      },
      {
        id: "2_3",
        from: 2,
        to: 3
      },
      {
        id: "2_4",
        from: 2,
        to: 4
      },
      {
        id: "4_1",
        from: 4,
        to: 1
      }
    ]
    var container = document.getElementById('mynetwork');
    var nodes = new vis.DataSet(allNodes);
    var edges = new vis.DataSet(allEdges);
    var data = {
      nodes: nodes,
      edges: edges
    };
    var options = {};
    var network = new vis.Network(container, data, options);
    intialize(network); //call this
    $("#btnSearch").on('click', function() {
      for (var i = 0; i < allNodes.length; i++) {
        if (allNodes[i].label.indexOf($("#inputSearch").val()) >= 0 && $("#inputSearch").val() != '') {
    
          allNodes[i].font = {
            background: "#FF0000",
            color: "#FFFFFF"
          };
        } else {
          delete allNodes[i].font;
        }
      }
      network = new vis.Network(container, data, options); //get instance
      intialize(network); //call this 
    });
    
    function intialize(network) {
      //reintialize..
      network.on('selectNode', function(properties) {
        console.log('Node is selected');
      })
      network.on('selectEdge', function(properties) {
        console.log('Edge is selected');
      })
    }
    #mynetwork {
      width: 500px;
      height: 500px;
      border: 1px solid lightgray;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script type="text/javascript" src="https://visjs.github.io/vis-network/standalone/umd/vis-network.min.js"></script>
    <input type="text" id="inputSearch" placeholder="Please enter the ip address"><button id="btnSearch">Search</button>
    <div id="mynetwork"></div>