I have a little problem, I must return a different choice of a select into a td
using thymeleaf, I try the next sentence:
You can do like that too
<p th:if="${projects != null and projects.size() gt 0}"> <a th:href="@{/hire/invite/} + ${f.id}" class="btn waves-effect">Anything</a> </p>
Otherthing
For next time if you want if - elsif -else
you can do:
(for example I want to know if url on my action object contains confluence then confluence
else if it contains jira then jira
on th:text)
<p th:text="((${#strings.contains({action.url},'confluence')}) ? 'confluence' : ((${#strings.contains({action.url},'jira')}) ? 'jira' : '')) " />
It works.
I think the solution is to use the switch statement, from Thymeleaf documentation:
<div th:switch="${user.role}">
<p th:case="'admin'">User is an administrator</p>
<p th:case="#{roles.manager}">User is a manager</p>
<p th:case="*">User is some other thing</p>
</div>
There isn't any other structure to use in Thymeleaf, although you could use th:if/th:unless. Check this thread in the thymeleaf forum.
I need else if but all my condition are not so simple that th:switch="${user.role} can works, so I do this:
<div th:switch="true">
<p th:case="${product.price} == '849'"> Beautiful Goat</p>
<p th:case="false"> Magnificent Goat</p>
<p th:case="'Whatever things I want to do with my else if'"> Goat</p>
</div>
You conditional operator contains 3 results. It should have 2 like this.
condition ? first_expression : second_expression;
In your situation. I assume linea.estado
is a boolean
value
<td style="white-space: nowrap">
<span th:class="${linea.estado} ? 'label label-success' : 'label label-danger'"
th:text="${linea.estado}? #{label.glineas.estado.iniciado} : #{label.glineas.estado.finalizado}">
</span>
</td>
If you want 3 values to be output and given that the linea.estado
is a string which may contain 'WARN', 'DANGER', 'INFO'
then you can do something like this.
<span th:class="'label label-' + ((${linea.estado} == 'SUCCESS') ? 'success' : (${linea.estado} == 'DANGER') ? 'danger' : 'warning')"
th:text="...">
</span>
But the cleaner solution will be something like this
<span th:if="${linea.estado} == 'SUCCESS'" class="label label-success" th:text="#{label.glineas.estado.iniciado}"></span>
<span th:if="${linea.estado} == 'DANGER'" class="label label-danger" th:text="#{label.glineas.estado.finalizado}"></span>
<span th:if="${linea.estado} == 'WARN'" class="label label-warning" th:text="#{label.glineas.estado.configurado}"></span>
Or using Switch
as mentioned by Patrick LC
If you want to have only one element the option is to make multiple conditions. It is confusing a little bit but if you understanda the logic it is not a big deal.
Lets say you want to do something like this:
if(user.role.admin){
//add only class='text-success'
}else if(user.role.user){
//add only class='text-primary'
}else{
//add only class='text-warning'
}
The logic is if1 : 'text-success' ? if2 : 'text-primary' ? 'text-warning'
Very simple. But the syntax would be like this:
<span th:class="( ${user.role.admin} ? 'text-success' : ( ${user.role.user} ? 'text-primary' : 'text-warning' ) )"></span>