<!DOCTYPE html>
<html>
<head>
<title>A single "Add Child" button for the root node</title>
<!-- Copyright 1998-2025 by Northwoods Software Corporation. -->
</head>
<body>
<div id="myDiagramDiv" style="border: solid 1px black; width:100%; height:400px"></div>
<textarea id="mySavedModel" style="width:100%;height:250px"></textarea>
<script src="https://cdn.jsdelivr.net/npm/gojs/release/go-debug.js"></script>
<script id="code">
const myDiagram =
new go.Diagram("myDiagramDiv", {
layout: new go.TreeLayout(),
"LayoutCompleted": e => {
const root = e.diagram.findTreeRoots().first();
if (!root) return;
if (root.findTreeChildrenNodes().count > 0) {
const b = e.diagram.computePartsBounds(root.findTreeChildrenNodes());
AddButton.position = new go.Point(b.x, b.bottom + 30);
} else {
AddButton.position = new go.Point(root.actualBounds.right + 30, root.actualBounds.y);
}
},
"undoManager.isEnabled": true,
"ModelChanged": e => { // just for demonstration purposes,
if (e.isTransactionFinished) { // show the model data in the page's TextArea
document.getElementById("mySavedModel").textContent = e.model.toJson();
}
}
});
const AddButton = // not a Node, so it is not laid out by the TreeLayout
new go.Part("Spot", {
selectable: false,
click: (e, part) => {
const root = e.diagram.findTreeRoots().first();
if (root) {
e.diagram.commit(diag => {
const childdata = { text: "New", color: "lavender" };
diag.model.addNodeData(childdata);
const linkdata = { from: root.key, to: diag.model.getKeyForNodeData(childdata) };
diag.model.addLinkData(linkdata);
});
}
}
})
.add(
new go.Shape("Circle", { fill: "whitesmoke", strokeWidth: 2, width: 36, height: 36 }),
new go.Shape("PlusLine", { strokeWidth: 2, width: 24, height: 24 })
);
myDiagram.add(AddButton);
// the design of the nodes doesn't really matter for this demonstration
myDiagram.nodeTemplate =
new go.Node("Auto")
.add(
new go.Shape({ fill: "white" })
.bind("fill", "color"),
new go.TextBlock({ margin: 8 })
.bind("text")
);
myDiagram.model = new go.GraphLinksModel(
[
{ key: 1, text: "Alpha", color: "lightblue" },
{ key: 2, text: "Beta", color: "orange" },
{ key: 3, text: "Gamma", color: "lightgreen" },
{ key: 4, text: "Delta", color: "pink" },
{ key: 5, text: "Epsilon", color: "yellow" },
],
[
{ from: 1, to: 2 },
{ from: 1, to: 3 },
{ from: 3, to: 4 },
{ from: 3, to: 5 },
]);
</script>
</body>
</html>