Tuesday, October 18, 2016

Thymeleaf dropdown with selected option

Thymeleaf suggests dropdown creation as follows:

<select th:field="*{type}">
<option th:each="type : ${allTypes}"
             th:value="${type}"
             th:text="#{${'seedstarter.type.' + type}}">Wireframe
</option>
</select>

However, if you want to have a 'selected' option this would not work:

<select th:field="*{type}">
 <option th:each="type : ${allTypes}"
              th:value="${type}"
              th:text="#{${'seedstarter.type.' + type}}"
              th:selected="${myCondition}">Wireframe
</option>
</select>

Instead you should do:

<select name="type">
<option th:each="type : ${allTypes}"
             th:value="${type}"
             th:text="#{${'seedstarter.type.' + type}}"
             th:selected="${myCondition}">Wireframe
</option>
</select>

3 comments:

  1. What is ${myCondition} there .Please Explain

    ReplyDelete
  2. It's any condition you like. For instance:
    th:selected="${a>b}">Wireframe

    ReplyDelete
  3. Play Baccarat, Craps, Blackjack - Free Play | FBCASINO
    Baccarat, 바카라 사이트 Craps, Blackjack. Play this game 인카지노 for free, in demo mode. No sign-in or download necessary! febcasino

    ReplyDelete