Centro Commerciale
Centro Commerciale

Reputation: 75

GoJS | How is it possible to retrieve the array of the selected nodes?

I'm using GoJs Flow builder and I've been trying to alert the IDs of the selected nodes (the blue ones) when clicking on a button inside the menu (that opens when you right-click on an object):

how to retrieve the array of the selected nodes

That is, I want it to alert "1,2,3" when clicked "Alert IDs".

Any help would be greatly appreciated!

Upvotes: 1

Views: 2471

Answers (1)

Walter Northwoods
Walter Northwoods

Reputation: 4106

  myDiagram.nodeTemplate.contextMenu =
    $(go.Adornment, "Vertical",
      $("ContextMenuButton",
        $(go.TextBlock, "Alert keys"),
        {
          click: function(e, obj) {
            var msg = "";
            e.diagram.selection.each(function(n) {
              if (!n instanceof go.Node) return;
              if (msg.length > 0) msg += ", ";
              msg += n.data.key;
            })
            alert(msg);
          }
        }
      ),
      $("ContextMenuButton",
      . . .

Read more about collections at http://gojs.net/latest/intro/collections.html.

Also, to answer your question more literally: myDiagram.selection.toArray() will return a JavaScript Array of selected Parts, on which you could use Array functions.

Upvotes: 2

Related Questions