Tapestry: default value for a dropdown component
I use the following code for a select-component:
Java-class:
@Component(parameters = {"blankOption=AUTO", "model=someModel", "value=someId",
"zone=someZone"})
private Select demoSelect;
Template:
<select t:id="demoSelect" />
This gets rendered to something like the following:
<select id="demoSelect" name="demoSelect">
<option value=""></option>
<option value开发者_StackOverflow社区="1">first</option>
<option value="2">second</option>
<option value="3">third</option>
</select>
The behavior I'm looking for is, that a certain option is preselected (this should be decided in the page class). How can I configure this in Tapestry? Basically I need to tell Tapestry to render the "selected" for the appropriate option, e.g.:
<select id="demoSelect" name="demoSelect">
<option value=""></option>
<option value="1">first</option>
<option value="2" selected="selected">second</option>
<option value="3">third</option>
</select>
Does it suffice to alter the model (I don't think so), or do I have to extend the Select-component itself. I have found this article, which looked quite promising, but unfortunately all links to the source codes are dead.
There is no need to extend anything. Just setting the property to a value before the rendering does the trick:
@Property
private SomeType someId;
@SetupRender
void initSomeId() {
if (this.someId == null) {
this.someId = this.getDefaultValueForSomeId();
}
}
Another simple way to fix this if you would like it to automatically default to the first option you can change the blankOption="AUTO"
to blankOption="NEVER"
.
This will remove the sometimes unnecessary initial blank space (Tapestry feature) of the dropdown. Useful to make it so the user is unable to just leave the selection empty and frees up exception handling.
精彩评论