So I’ve got my drop down list populated and sorted.
Now I want to insert a “selected” value. How do I do that?
Let’s return to our previous example: a drop down list of calendar months, sorted by ordinal.
In that example, we had a pair of drop down controls — one for month and one for year. I’d like to have the current month and the current year selected by default.
As before, we’ll focus on the month control for the discussion.
The Drop Down List Control Properties
The interface for the Data Source region of the Properties window for the month drop down list control seems to have all kinds of options there for us to play with —
— but the oasis of configuration is for us only a mirage. There’s nothing here for setting a selected value.
Different from Filtering
Perhaps I should clarify the intention. We can use this interface to filter the options the drop down list presents — meaning, if we had one SmartObject (SmO) with all 12 months as the basis for our drop down, and another SmO that listed only January, February and March, we could use the second SmO to filter the results of the first, so that our list would only show January February and March. But we don’t want a filter — we want to show all 12 months as options, but just have the current month as the selected option within the control.
Going back to our HTML illustration:
If the current month is April, the option tag would contain the selected attribute, like this:
… which is different from filtering.
The Rule Designer
Actually, we don’t want to be anywhere within the layout at all. For us to implement a default selection, we need to go to the Rule Designer.
— But in order to realize the selection of the current month, we need something else first — an expression. While having some is critical to the scenario, it’s not really necessary to achieve the goal of this post. If you’re interested in how to create one, please consider reading this post. Okay. Back to the Rule Designer.
We’re particularly interested in the “When the View executed Initialize” rule (for our view, imaginatively named “Report”). The rule itself is created for us by default — we want to edit it.
The K2 Designer also created for us a rule for populating our month drop down list with data.
We’re going to add another action — a Transfer Data Action. Note that this is not a drag-and-drop operation — just select “Transfer data” from the Select an Action region (on top) and the new rule will be placed in the Rule Definition region (at bottom).
Configuring the new action is where two expressions come in: one called “Get Current Month”, and one called “Get Current Year.” Both simply derive the data we want from the current value of Now() at runtime. Put simply, we’re going to map the Get Current Month expression to the Report Month drop down list control, and map the Get Current Year expression to the Report Year drop down list control — like this:
With these mapped (after several OKs and Finishes), run your view. You’ll see the drop down lists jump to the current values — still retaining all of the original list options!