Send to back - Gojs

Hi,
I am trying to implement send to back and bring to front functionality. While the below code works fine for bring to front, but has some issues with send to back. For instance i have 2 shapes, and i give go to back command for the top shape and it goes back without any issue. Now the new object which is in front doesnt respond to go to back. Kindly guide me.

function makeContextMenu(wiDiagram)
{
var $ = go.GraphObject.make
return $(go.Adornment, “Vertical”,
$(“ContextMenuButton”,
$(go.TextBlock, “Bring to front”),
{
click: function (e, obj)
{

                obj.part.adornedPart.layerName = "";
                obj.part.adornedPart.layerName = "Foreground";

               
            }
        }
    ),
    $("ContextMenuButton",
        $(go.TextBlock, "Send to Back"),
        {
            click: function (e, obj) {
               

                obj.part.adornedPart.layerName = "";
                obj.part.adornedPart.layerName = "Background";


            }
        }
    )

);
}

Your code does put the second node in the “Background” layer, but because the first node is also in the “Background” layer, the second node happens to be in front of the first node.

Instead of using layers, use Part | GoJS API. Note that this assumes that all Nodes are in the same Layer.

Look at the last example on the Introduction page GoJS Layers -- Northwoods Software.

Thanks walter, i will try it and let you know how it turns out.

Hi Walter
$(“ContextMenuButton”,
$(go.TextBlock, “Send to Back”),
{
click: function (e, obj) {
obj.part.adornedPart.data.zOrder = (obj.part.adornedPart.data.zOrder || 0) + 1;
The above code sets the zOrder but it doesnt reflects in the output.

Do you have a Binding on Part.zOrder?

As documented at GraphObject | GoJS API, you need to execute changes within a transaction.

If you want to modify data, you need to call Model | GoJS API.

http://gojs.net/latest/intro/dataBinding.html#ChangingDataValues

Thanks walter, after binding it works fine. Can i get the highest zOrder value in a given layer?

You need to maintain the maximum and/or minimum values yourself, if you care about those values.

yes, that did the trick!!!