Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 239
martin Avatar asked Jul 21 '11 10:07

martin


1 Answers

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();
    }
}
like image 74
Henning Avatar answered Sep 21 '22 15:09

Henning