Best approach for multi-column (cumulative) sorting in Data Grid 2 — reusable across entities?

0
I'm looking for the recommended way to implement multi-column sorting in Data Grid 2, and I'd appreciate feedback on my planned approach.Users need to sort a grid by more than one column at the same time. For example: sort by Product Name ascending, then by Size, then by Quantity — and all three sort levels should stay active together. Right now, sorting by a second column clears the sort on the first one (only a single sort criterion is kept at a time). The customer also requires that on a grid with, say, 20 columns, every column must be sortable.1.Cumulative multi-column sorting (primary / secondary / tertiary … in a user-defined order).2.A generic, reusable solution that works across multiple grids, each backed by a different entity / data source, without rebuilding the logic for every grid.
asked
2 answers
1

Hi Bosung Kim,


If the requirement is that users can build a cumulative sort order (e.g. Product Name → Size → Quantity), I don't believe Data Grid 2 currently supports Excel-style multi-column sorting through repeated header clicks out of the box. There is an Idea request asking for this capability.


If a default multi-column sort is sufficient, you can configure multiple sort attributes directly in the Data Grid 2 datasource settings, for example:

ProductName ASC
Size ASC
Quantity DESC

This works for database and XPath data sources.


For a reusable solution across different entities, I would store the user's sort preferences in a generic entity (Attribute, Direction, Sequence) and retrieve the data through a datasource microflow. This avoids creating custom logic for every grid and gives you flexibility to persist user preferences.


Store the user's selected sort criteria and drive the grid from a datasource microflow (or query layer). This keeps the implementation generic and avoids entity-specific sorting logic.


One additional consideration is user experience. If multiple sort levels are active, users should be able to see the order (for example: Product ↑ (1), Size ↑ (2), Quantity ↓ (3)) so the resulting sort remains understandable.


I hope this helps answer your question. If you need any additional details, feel free to reply below.


Best regards,

Oswaldo Valles

answered
1

Hi,


For this requirement, there are two different things to distinguish: a predefined multi-column sort order and user-driven cumulative sorting.

Data Grid 2 supports sorting per column, but the standard column-header interaction does not provide a Shift-click style multi-column sort where the user can keep adding sort levels. So I would not try to work around this by creating separate microflows for every possible column combination.

If the sort order is known beforehand, you can configure multiple attributes in the Sort Order of the data source. Mendix supports multiple sort criteria, and they are applied in the order in which they are defined. For example:

ProductName ASC
Size ASC
Quantity DESC

This gives the expected primary → secondary → tertiary sorting behavior.

For the second requirement — where the user should dynamically build something like:

Product Name ↑
Size ↓
Quantity ↑

and keep all three levels active — the standard Data Grid 2 UI does not expose that interaction.

However, this is possible at the pluggable-widget API level. The ListValue API has a setSortOrder() method which accepts an array of SortInstructions, so multiple sort criteria can be applied at the same time. Mendix's current documentation even shows an example of sorting by two attributes simultaneously.

Conceptually, the widget can maintain something like:

[
    [ProductName, "asc"],
    [Size, "desc"],
    [Quantity, "asc"]
]

and pass that array to:

dataSource.setSortOrder(sortInstructions)

The important part is that the attributes need to be sortable; the API exposes the sortable flag for this purpose.

For a reusable solution across different entities/grids, I would therefore avoid hard-coding entity-specific sorting logic in microflows. A custom pluggable widget can expose the grid's sortable attributes, maintain the user's sort priority/order, and call setSortOrder() with the resulting list.

One other advantage is that for Database/XPath data sources, the sorting is handled by the backend, so you are not expected to retrieve the complete dataset and sort it yourself in JavaScript.

So, in short:

  • Fixed multi-column sorting: configure multiple sort items in the data source.
  • User-defined cumulative sorting: not available through the standard Data Grid 2 header interaction.
  • Reusable dynamic implementation: a pluggable widget using ListValue.setSortOrder() is the appropriate extension point.
  • I would not create separate microflows for every possible column combination; that becomes difficult to maintain once the grid has 20+ sortable columns.

This also keeps the sorting at the datasource level instead of implementing a client-side sort on the displayed rows, which is important when paging/large datasets are involved.

important point: setSortOrder() actually accepts multiple sort instructions.he limitation is specifically the standard DG2 user interaction, not the underlying datasource API.

Data Grid 2 documentation

Mendix ListValue / setSortOrder API documentation

answered