Tapestry: default value for a dropdown component
Asked Answered
M

2

5

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="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.

Monobasic answered 21/7, 2011 at 10:4 Comment(0)
H
7

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();
    }
}
Hedwighedwiga answered 21/7, 2011 at 11:49 Comment(1)
Thanks, I just overlooked this straightforward way.Monobasic
R
1

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.

Robber answered 1/9, 2015 at 13:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.