Sorting dropdownlist on Enum caption

0
Hello, I have the following scenario: Entity with an attribute called name which is an enumeration. In the front-end I have a dropdownlist where I would like to sort on the caption of the name attribute. I could not get this to work. As far as I understand when you set sortorder to an enum it sorts by key, however the keys have no logical alphabetic order, so this does not result in a sorted list. I have tried removing the sortorder alltogether from the dropdownlist and manually rearange the enumeration entries so that the keys are scattered but atleast the captions are sorted but this did not result in a sorted list either (in the front-end). The only solution I can think of now is creating leading dummy characters such as AA_[enumkey] AB_[enumkey] and then sort by name or perhaps creating a new attribute displayname in the given enitiy and sort on that one. Both will take up a lot of time by migrating and creating the new logic when creating and deleting these objects.. Is there any alternative solution? Thanks in advance,   Selwyn de Jonckheere  
asked
4 answers
0

Hi, I think you can use a reference selector in combination with some helper objects for this. Double click on the reference selector, go to tab selectable objects and choose the microflow option as a source.

Also create a helper object which also includes the name caption of the enumeration and associate this object with your 'main' object which you are displaying on the page.

You can now retrieve the helper objects in your selectable objects source microflow and sort them here.

answered
0

Hi Selwyn,

You are correct that changing the enumeration keys to include alphabetical prefixes is not a good long-term solution.

Mendix does not provide a direct runtime sorting option to sort an enumeration dropdown by its translated caption. A better approach is to use a helper entity with a Reference Selector.

Recommended solution: Helper Entity + Reference Selector

1. Create a helper entity

For example, create a non-persistent entity called EnumOption with these attributes:

  • EnumKey – String

  • DisplayName – String

You can also use an enumeration attribute in the helper entity if you want to retain the original enum value.

2. Create a data source microflow

Create a microflow that:

  • Retrieves or creates the helper objects for all enumeration values.

  • Assigns the appropriate display caption using getCaption().

  • Returns the helper objects sorted by DisplayName ascending.

For example:

EnumKey       DisplayName
Active        Active
Completed     Completed
InProgress    In Progress
Pending       Pending

3. Configure a Reference Selector

On your page:

  • Use the helper entity as the selectable objects source.

  • Configure the source as a microflow.

  • Set the caption to DisplayName.

  • Configure sorting by DisplayName ascending.

4. Update the original enumeration attribute

When the user selects a helper object, use an On Change microflow to map its EnumKey back to the corresponding enumeration value and update the original entity's enumeration attribute.

For example:

If $EnumOption/EnumKey = 'Active'
    Set Status = Module.Status.Active

If $EnumOption/EnumKey = 'Completed'
    Set Status = Module.Status.Completed

Make sure to handle empty selections and all possible enum values.

Alternative

If you only need a fixed order and do not need dynamic sorting based on translations, you can manually arrange the enumeration values in Studio Pro in the desired order. However, this is not the same as dynamically sorting by caption.

My recommendation: Use the helper entity approach if the captions must be sorted alphabetically, especially when the application supports multiple languages. It keeps the original enumeration keys unchanged and avoids dummy prefixes or database-wide migration.

References:

Hope this helps! If you find this answer helpful, please mark it as the accepted answer.

answered
0

Hi Selwyn,

One possible workaround is to use a helper entity with a String attribute to store the enumeration captions. You can populate this attribute with the corresponding caption values and use it for alphabetical sorting in the dropdown.

This avoids modifying the existing enumeration keys or migrating your current entity data. It may require some additional logic to maintain the helper records, but it keeps the existing data structure intact.

Hope this helps!

answered
0

You can try using Combo box with below settings and mention the sort order in enumeration where it is defined.

answered