Removing an item by ID (jointJS)

I noticed that JointJS links can be removed by hovering over them and clicking on the big red X that appears. But I was wondering if it is possible to delete an element after its creation , without knowing the name of the variable.

onCreateButtonClick(function(){
  var rect = new joint.shapes.basic.Rect({
    position: { x: 100, y: 30 },
    size: { width: 100, height: 30 }
  });
  graph.addCell([rect]);
});

onRemoveButtonClick(function(){
   //removeRectangle here?
});

My question is: can I remove this rectangle in the second function?

+4
source share
2 answers

Removing elements by ID, you can simply perform as: graph.getCell(cellID).remove(). In onRemoveButonClick()you need to somehow find out which item you want to delete. It depends on your application user interface, but you can, for example, do something like:

var selected;

paper.on('cell:pointerdown', function(cellView) {
    selected = cellView.model;
});

onRemoveButtonClick(function() { 
    if (selected) selected.remove(); 
});
+7
source

, cellView .

paper.on('cell:pointerclick', function(cellView, evt, x, y) {
    cellView.remove();
});
0

All Articles