Is there a way to copy selected nodes of GoJs diagram upon button click?

I want to copy selected nodes of a gojs diagram from one canvas upon button click and then paste it onto another canvas, is it possible ?

Sure. Add this to your node template:

      click: function(e, node) { e.diagram.commandHandler.copySelection(); }

Note that if the user control-clicks or shift-clicks on the node, the selection may have multiple nodes in it, so that when pasting GoJS will copy multiple nodes.

Caveat: this only works for two (or more) Diagrams in the same page or iframe. If you want it to work between pages or after the user logs out, you may want to save the clipboard in localStorage. Use this:

Hey Walter,

Thanks for your reply,

This is helpful, but i want the copy the nodes upon a custom button click, and not by clicking on the nodes, also i want to paste the copied nodes to a different canvas upon similar custom button click.

Oh, you meant an HTML button click? Sure, just select the nodes that you want and call CommandHandler.copySelection. Then call CommandHandler.pasteSelection in the other diagram.

Depending on whether the two diagrams share the same JavaScript environment, where the clipboard is stored is still an issue.

I tried this -


but there is an error -

Uncaught TypeError: Cannot read property ‘copySelection’ of undefined

You need to spell the property correcly.

I am sorry but Is there anything that i’ve missed out ? The spellings are as it is there in the documentation.

I have referred to this link:

This error:

Uncaught TypeError: Cannot read property ‘copySelection’ of undefined

Means that whatever is trying to reference copySelection does not exist

does not exist because the correct spelling is:
with a lowercase c

myDiagram.commandHandler.copySelection(); is what you need to write.

It worked, Thanks for your help Simon.