GoJS树形图配置问题:无需Comparer实现新增节点始终添加至列表最右侧
Hey there! Let's tackle this problem. The default behavior of GoJS's TreeLayout (with angle: 90, which arranges siblings horizontally below the parent) places new nodes to the left only if they're added as the first sibling in the model. Here are two straightforward ways to fix this without needing a custom Comparer:
1. Append New Links to the End of the Model's Link Data Array
The order of sibling nodes in the tree is determined by the order of their link entries in the linkDataArray. If you're currently inserting new links at the beginning of this array, that's why new nodes show up on the left.
Instead, append new link data to the end when adding a child node. For example:
// Assuming diagram is your GoJS Diagram instance const model = diagram.model; model.commit(m => { // Add new node data const newNodeKey = ...; // your new node's unique key m.addNodeData({ key: newNodeKey, label: "New Node" }); // Append the link data to the end (don't prepend it) m.addLinkData({ from: parentNodeKey, to: newNodeKey, key: ... }); }, "Added new child node");
This ensures the new child is the last in the sibling order, so TreeLayout places it on the right automatically.
2. Use reverseChildren in TreeLayout
If you can't control the order of links in the model (e.g., data comes from an external source in a fixed order), you can reverse the sibling arrangement directly in the TreeLayout config.
Just add reverseChildren: true to your TreeLayout settings:
layout: $(go.TreeLayout, { angle: 90, nodeSpacing: 100, reverseChildren: true // Flips sibling order to right-to-left }),
This changes the default left-to-right sibling order to right-to-left. So the first sibling in the model becomes the rightmost, and any new nodes added as first siblings will now appear on the right. Note this affects all sibling groups in your tree, not just new ones.
Either of these approaches will get your new nodes showing up on the right without needing a custom Comparer function.
内容的提问来源于stack exchange,提问作者Yaniv Suriyano

