如何使用 thymeleaf 条件 - if - elseif - else [英] How to use thymeleaf conditions - if - elseif - else

查看:255
本文介绍了如何使用 thymeleaf 条件 - if - elseif - else的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题,我必须使用thymeleaf将一个select的不同选择返回到一个td,我试试下一句:

I have a little problem, I must return a different choice of a select into a td using thymeleaf, I try the next sentence:

<td style="white-space: nowrap">
    <span th:class="${linea.estado}? 'label label-success' : 'label label-danger' : 'label label-warning'"
          th:text="${linea.estado}? #{label.glineas.estado.iniciado} : #{label.glineas.estado.finalizado} : #{label.glineas.estado.configurado}">
    </span>
</td>

但是我有一个问题,因为条件给了我一个很大的失败,因为无法解析表达式.只有两个条件(iniciado 和 finalizado)没有问题,但我需要为表单中的选择获取正确的标签.

But I have a problem because the condition give me a big failure because is impossible to parse the expression. With only two conditions (iniciado and finalizado) there aren't problem, but I need to get the correct label for the select in my form.

有人知道在 thymeleaf 中使用 if elseif else 语句的正确句子吗?

Anybody knows the correct sentence to use a if elseif else sentence with thymeleaf?

2.0 现在我正在尝试用下一个来解决这个问题:

2.0 Now I am trying to solve this problem with the next:

<td style="white-space: nowrap">
    <span th:if="${linea.estado} == 'Iniciado'" class="label label-success" th:text="#{label.glineas.estado.iniciado}"></span>
    <span th:if="${linea.estado} == 'Finalizado'" class="label label-danger" th:text="#{label.glineas.estado.finalizado}"></span>
    <span th:if="${linea.estado} == 'Configuracion'" class="label label-warning" th:text="#{label.glineas.estado.configurado}"></span>
</td>

解决方案是完美的,现在一切正常.谢谢大家.

The solution is perfect, now all works properly. Thanks for all.

推荐答案

您的条件运算符包含 3 个结果.应该有 2 个这样的.

You conditional operator contains 3 results. It should have 2 like this.

condition ? first_expression : second_expression;

在你的情况下.我假设 linea.estado 是一个 boolean

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>

如果您想要输出 3 个值,并且 linea.estado 是一个字符串,其中可能包含 'WARN', 'DANGER', 'INFO' 然后你可以做这样的事情.

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>

但是更干净的解决方案将像这样

<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>

或者使用Patrick LC

  • 注意语法错误,因为我没有在运行时测试任何代码

这篇关于如何使用 thymeleaf 条件 - if - elseif - else的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆