请求修复getorgchart中MIXED_HIERARCHY_RIGHT_LINKS布局排序异常问题
Got it, let's fix this reversed ordering issue with the MIXED_HIERARCHY_RIGHT_LINKS layout when using your XML data source. Here's how to tackle it step by step:
1. Adjust the Layout's Sorting Behavior
First, head into your index.html file where you initialize the MIXED_HIERARCHY_RIGHT_LINKS layout. The problem here is that this layout defaults to rendering non-top rows in reverse order—we need to override that sorting rule.
If you're using a library like GoJS (common for hierarchical layouts), modify your layout initialization to enforce forward sorting (either keeping the XML's original order or sorting alphabetically):
// Replace your existing layout setup with this const layout = new go.MixedHierarchyLayout({ direction: 90, // Keeps the right-link orientation arrangement: go.MixedHierarchyLayout.ArrangementRightLinks, // Force forward sorting to preserve original/alphabetical order sorting: go.Layout.Forward, // Optional: Add a custom comparer for strict alphabetical sorting comparer: (nodeA, nodeB) => nodeA.data.label.localeCompare(nodeB.data.label) });
2. Pre-Sort XML Data (If Layout Doesn't Support Custom Sorting)
If your layout library doesn't have a built-in sorting option, pre-process the XML data before feeding it to the layout. In index.html, after parsing init-from-xml.xml, sort the nodes explicitly:
// Parse your XML file first const xmlDoc = new DOMParser().parseFromString(xmlContent, "text/xml"); const itemNodes = Array.from(xmlDoc.querySelectorAll("item")); // Sort nodes alphabetically by their label attribute const sortedItems = itemNodes.sort((a, b) => { const labelA = a.getAttribute("label"); const labelB = b.getAttribute("label"); return labelA.localeCompare(labelB); }); // Now use sortedItems to generate your layout nodes instead of the raw XML nodes
3. Double-Check Your XML Structure
Make sure init-from-xml.xml already has items in your desired alphabetical order—if the source XML is out of sequence, even sorting logic might not fix it. A properly ordered XML should look like this:
<root> <item label="Apple" /> <item label="Banana" /> <item label="Cherry" /> <item label="Date" /> </root>
After making these changes, your layout should display the top row at the top, with all remaining rows ordered correctly from top to bottom (matching your XML or alphabetical sequence) instead of being reversed.
内容的提问来源于stack exchange,提问作者lool10

