Just a tip about Thymeleaf 3 select / option / th:selected in a list

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Just a tip about Thymeleaf 3 select / option / th:selected in a list


The interesting part is the tag th:selected :
-  when you load the page the first time, the object regalim can be null, and so you can select a default option ( 'NA' ) by the first argument of the "?" operator ;
-  when you have loaded an object in a page, regalim may be not null,  you can select the good option by the second argument of the "?" operator (${regA.nom}==*{regalim})

Hope that can help somebody.
pastjl
Hello this is my first post!. I just beging with Spring Boot / Thymeleaf

After some issues I have encountered, and reading some posts in this forum about subject above,  I finally succeeded with this kind of template ( extract of a post form) . Don't use th:field with th:selected !!

<select  id="regalim" name="regalim">
        <option th:each="regA : ${regAList}" th:value="${regA.nom}"    th:text="${regA.nom}"
        th:selected="(*{regalim} == null? ${regA.nom}=='NA' :${regA.nom}==*{regalim})">                                                                                         
        </option>   
                                               
</select>