How to detect Which node is Deleted from Diagram through ChangedEvent

I have a diagram having multiple nodes, if I deleted one node from the diagram how to detect which node I deleted is there any method for that?

Thankyou

For deletions that the user does (typically by selecting some nodes and pressing the Delete key) you can register a “SelectionDeleted” DiagramEvent listener.

  $(go.Diagram, . . .,
    { . . .,
      "SelectionDeleted": function(e) {
        . . . e.subject is the collection of deleted Nodes and Links . . .
      }
    })

For all programmatic removals of nodes from the model, whether user-initiated or in your code, you can establish a Changed listener on the Model to look for a ChangedEvent that is a removal of data from the Model.nodeDataArray.

  $(go.Diagram, . . .,
    { . . .,
      "ModelChanged": function(e) {
        if (e.change === go.ChangedEvent.Remove && e.modelChange === "nodeDataArray") {
          . . . e.oldValue will be the removed node data object . . .
        }
      }
    })

But you probably want to send updates to the server all at once, not one at a time. Please read GoJS Changed Events -- Northwoods Software

$(go.Diagram, . . .,
{ . . .,
“SelectionDeleted”: function(e) {
. . . e.subject is the collection of deleted Nodes and Links . . .
}
})
Thank you for your reply,
But, I want to delete the data from the database for that particular node … But, I am unable to fetch the data from the node. So, how I can get the exact node data from the deleted node.

I got the answer from here

Thanks @walter

“SelectionDeleted”: function(e) { e.subject.each(function(p) { console.log(p.part.data); }) }

HI @walter
is there any way to prevent delete after clicking the delete key or restore the deleted data programmatically

I am trying to give user a warning before delete

The right thing to do is to customize CommandHandler.canDeleteSelection to return false in those circumstances.

See the example in the change log.

@walter I am getting this below error

Error: Unknown DiagramEvent name: commandHandler.canDeleteSelection

this.diagram.addDiagramListener(“commandHandler.canDeleteSelection”,
e => {
this.modelDeleted.emit(e);
});
using in angular 4

That example code is in Diagram initialization with GraphObject.make, not a call to addDiagramListener.

this.diagram.addDiagramListener(“ChangedSelection”,
e => {
const node = e.diagram.selection.first();
this.nodeSelected.emit(node instanceof go.Node ? node : null);
});

This code is working for me so the same way am implementing for

this.diagram.addDiagramListener(“commandHandler.canDeleteSelection”,
e => {
this.modelDeleted.emit(e);
});

and it’s through me this error

Error: Unknown DiagramEvent name: commandHandler.canDeleteSelection

@walter let me know what I need to change?

If you don’t want to override the CommandHandler method when building the Diagram using GraphObject.make, that’s OK. You can do it later via:

this.diagram.commandHandler.canDeleteSelection = function() { ...

Hi @walter

is there any another way to delete a node other than commandHandler.deleteSelection();

There can be lots of ways to remove nodes in your code. Please read again what I wrote first in this topic: How to detect Which node is Deleted from Diagram through ChangedEvent - #2 by walter