almende / vis

⚠️ This project is not maintained anymore! Please go to https://github.com/visjs
7.85k stars 1.48k forks source link

Calling a custom method from the manipulation at delete node #4084

Open Prat041192 opened 6 years ago

Prat041192 commented 6 years ago

Hi community, I have been playing around with vis.js for a while now and have landed up in a problem. Background: I am trying to use vis.js (v: 4.21.0) with Angular4 (v: 4.2.4). Issue: I want to call a custom method before deleting the node, so I added a method to delete node in manipulation, But I am unable to call my custom method from this, Refer code I have added comments to explain myself .

Code: My-component.ts

export class MyComponentComponent implements OnInit {

  private graphData: any;
  private options: any;

  constructor() { }

  ngOnInit() {
    let nodeList = 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
  let edgeList = new Vis.DataSet([
      {from: 1, to: 3},
      {from: 1, to: 2},
      {from: 2, to: 4},
      {from: 2, to: 5}
  ]);
  this.graphData = {nodes: nodeList, edges: edgeList};

  this.options = {
    manipulation: {
      enabled: true,
      initiallyActive: false,
      addNode: true,
      addEdge: true,
      editEdge: true,
      deleteNode: function(nodeData,callback) {
        debugger
        this.deleteNodeFunction(nodeData); // defined below
         `here getting issue ''this'' is not defined.`
      },
      deleteEdge: true,
    }
  };

  let network;

  try {
     const container = document.getElementById('test');
    debugger;
    network = new Vis.Network(container, this.graphData, this.options);

  }catch (e) {
    console.log(e.message)
  }
  }

  deleteNodeFunction(nodeData){
// do something here 
// may be call some other other method.
    debugger
  }

}

Any help or suggestions how can this be achieved.

fdambrosio commented 6 years ago

have you found a solution? I have a similar problem

Prat041192 commented 6 years ago

@fdambrosio No i have not. but I had a work around playing with groups for my Issue. But still kept the issue open if in case some one has a proper solution.