The IfElse Widget — Mendix

Vinicius Strugata Ambrosio
3 min readMay 20, 2023

--

Sometimes it’s required to show/hide a widget depending on a given condition. Mendix has a standard way to accomplish this requirement and works like a charm. However, there are situations where the IfElse widget is a better solution.

Photo by Einar Storsul on Unsplash

Almost all Mendix visual widgets allow us to define a boolean attribute or expression to indicate its visibility. While this works most of the time, such a widget must be nested on a DataView. The IfElse widget doesn’t have this limitation and clearly indicates visually what will be shown or hidden at design time. This makes the code more organized and maintainable. In addition, it doesn’t require the need to write the opposite of the true condition, avoiding logic code duplication.

Use Case

Let’s suppose a menu bar that needs to be displayed accordingly with a condition. Such a condition is stated as a constant value: if odd then show a menu, else if even show another menu. The result must be like is shown in the figure below:

Menu to be shown if the constant is odd
Menu to be shown if the constant is even

This can be achieved using user role permissions (in a Page or Microflow/Nanoflow), but for the sake of this example, let’s consider that this is not possible for some reason. And since the menu is positioned on the Layout of the page, it’s not possible to use conditional visibility either, because we cannot use Dataviews in a Page Layout.

Solution

The IfElse widget comes in handy in some cases because it gives us the possibility to define a simple boolean expression that controls the visibility of the then and else containers.

The IfElse widget requires only a boolean expression to work

And, for this particular case, the solution was to combine the usage of the IfElse widget in a Page Layout with two Menu Documents: one for the odd value and another for the even value.

The IfElse widget only requires a boolean expression
The Odd Menu and Even Menu definition

If you define the constant CTE_OddOrEven as 1, the first menu is shown in the then container. The else container shows the second when CTE_OddOrEven is 2, for example.

Conclusion

This example use case is very simple, I must admit. But bear in mind the beauty of the IfElse widget:

  • Requires only a boolean expression to work.
  • You don’t need to write the opposite condition, which is a requirement when you use the traditional Conditional Visibility feature.
  • The widget to show/hide doesn’t need to be inside a Dataview. However, it’s also possible to use it with a surrounding Dataview. That is the scenario where you don’t need to write the opposite condition.
  • You don’t need to use Non-persistent Entities only to control the visibility of such widgets.
  • Since we cannot use Conditional Visibility inside a Page Layout, it gives us the flexibility to create a complex layout only using boolean expressions.

Kudos to Objectivity for providing us with such a simple yet powerful widget!

Links & References

  1. Mendix Marketplace — IfElse widget: https://marketplace.mendix.com/link/component/117556
  2. Sandbox Demo App: https://objectivitycommons-sandbox.mxapps.io/
An example of the IfElse widget

--

--