You can present a fixed set of text choices in a combobox by binding a String value in your ViewModel to an OCL collection of strings; use this when users must choose one of several known labels.
What you create
A combobox is a dropdown, also called a picklist, that lets the user select one value. In this pattern:
- A String value in the ViewModel stores the selected text.
- An OCL expression creates the strings displayed in the combobox.
- The combobox pick list uses that expression.
For example, a ViewModel variable named OrderColumnName can hold the column by which an order list is sorted. The user can choose Name, Date, or Status from the dropdown.
Create a fixed string pick list
- In MDriven Designer, create or select the ViewModel value that will hold the selection. Its type must be
String. - Add a ViewModel column that exposes that String value. For this example, use
OrderColumnName. - Configure that column as a combobox and set its pick-list expression to an OCL
Sequencecontaining the available strings. - Run the ViewModel and select an item. The selected text is assigned to
OrderColumnName.
Use an expression such as:
Sequence{'Name', 'Date', 'Status'}The expression returns a collection of three strings. The combobox displays those strings, and the selected string becomes the value of OrderColumnName.
Example: choose an order column
Suppose a ViewModel uses OrderColumnName to remember which order field the user chose. Configure its combobox pick list with:
Sequence{'OrderNumber', 'CustomerName', 'OrderDate'}When the user selects OrderDate, the ViewModel value becomes OrderDate. Use that value in the ViewModel logic that needs the selected column name.
Build a conditional list
You can create a list whose entries depend on expressions. Use Sequence when order matters, or Set when you need a set of strings. For example, this expression adds a label only when its related value is available:
Sequence{
self.ValidMaterials->notEmpty.caseTrueFalse('Materials', String.nullValue),
self.ValidColors->notEmpty.caseTrueFalse('Colors', String.nullValue),
self.ValidFinish->notEmpty.caseTrueFalse('Finish', String.nullValue)
}->asCommaListSee Documentation:Collection of strings for more examples of creating string collections with Sequence and Set. If the selected string controls conditional logic, use the string-comparison guidance in Documentation:Comparing strings.
Keep the selected value available
The current value of the String attribute or ViewModel variable must be present in the combobox collection. If it is absent, the combobox can lose its displayed selection or show an incorrect selection when the view opens.
For example, if OrderColumnName currently contains OrderDate, this pick list is valid:
Sequence{'OrderNumber', 'CustomerName', 'OrderDate'}This pick list is not valid for that current value because it omits OrderDate:
Sequence{'OrderNumber', 'CustomerName'}Also ensure that the collection used as a pick list is stable over time. A collection can appear to contain the same choices while being reconstructed as new objects; comboboxes work with objects rather than only their displayed values. For a list that must remain stable, create it once for the ecospace and reuse it, or store it in the database when appropriate.
When to use another combobox pattern
Use this pattern for a small, known list of String values. When users select a reference to another model object, configure an object pick list instead; see Documentation:The combobox. For MVC views that bind object references through external IDs, use Documentation:Comboboxes in MVC from model driven ViewModel.
