Removing an element by ID (jointJS)
Asked Answered
R

2

5

I noticed that JointJS links can be removed by hovering over them and clicking the big red X that appears. But I was wondering if it is possible remove an element once it has been created, without knowing the variable name.

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?

Rouvin answered 11/12, 2013 at 23:11 Comment(0)
S
8

Removing elements by ID can simply be done as: graph.getCell(cellID).remove(). In your onRemoveButonClick(), you have to somehow know which element you want to remove. This depends on you application UI 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(); 
});
Seritaserjeant answered 12/12, 2013 at 14:59 Comment(1)
graph.getCell(cellID).remove() does not remove a cell if the cell has children. I get this exception when I try to remove a cell that has children - Uncaught TypeError: Cannot read property 'unembed' of undefined How to delete all the cells of a graph, embedded and un-embedded?Pinfold
T
1

I implemented the removal of an element by single clicking the element , using the cellView arguement directly.

paper.on('cell:pointerclick', function(cellView, evt, x, y) {
    cellView.remove();
});
Tardiff answered 20/10, 2016 at 6:24 Comment(3)
How to save it to json dynamically?Dentalium
Do you mean to ask how to update the json for removed element ? Are you maintaining the json for paper manually ?Tardiff
Yes, when ever i change or remove element i want to save it to JSON ans send to the serverDentalium

© 2022 - 2024 — McMap. All rights reserved.