Save json with nodes order

Hi Walter.

In a BPMN like example, I’m having one Pool and Lanes (steps) each having nodes of all kind types (activity, gateways). Whenever I save the diagram I need to convert the JSON into a custom model/json where order (index) of each one is very important (there’s a system which executes those based on their order). Unfortunately I can’t see in GoJs how easily to extract/convert the model to our custom json with nodes order/index. Even “pool.memberParts” doesn’t return the visual order. Seems like I have to sort by Location.y to get their order.

Any ideas how to accomplish this easier ?

I just found this [SOLVED] Get the sort order of the Swimlanes - #7 by simon
Would that mean that I need to keep&save the index myself manually based on a “loc” sorting function ?

In Pool layout, something like:

    getLanesByOrder() {
        const pool = this.group;
        const lanes = [];

        pool.memberParts.each( (lane) => {
            if (!(lane instanceof go.Group)) return;
            lanes.push(lane);
        });

        return lanes.sort((lane1, lane2) => {
            const y1 = lane1.location.y;
            const y2 = lane2.location.y;

            if (y1 < y2) {
                return -1;
            }
            if (y1 > y2) {
                return 1;
            }
            return 0;
        });
    }

Then loop each lane memberParts and each node with findNodesOutOf and so on…

Are you sure you want to get the nodes ordered only by Y location? What if the nodes are moved by the user up or down? Doesn’t your app actually care more about the links – dependencies – between the nodes?

Only Lanes are filtered by Y location as that’s the only variable that decides the order (user my DnD and re-order them). But the Nodes in Lanes of course are sorted/ordered by “node.findNodesOutOf” and some checks.

I think the approach you outlined above could work.

Yep, I just built the final JSON already. Was a little surprised and disappointed that there’s no easy and built in way in GoJs to export the model JSON with the correct order in arrays.

Thanks for feedback (keeping best practices is very important).

Any notion of “correct” order is inherently specific to particular applications. I bet other policies would also be reasonable.

I suppose you could try sorting the Model.nodeDataArray before calling Model.toJson.

1 Like

A post was split to a new topic: Quick shift when starting to drag a node