Thymeleaf classappend for multiple classes

2020-04-03 09:37发布

I want to add multiple classes using condition.

<div th:classappend="x.isTrue ?'class1' "  ></div>

I want something like

<div th:classappend="x.isTrue ?'class1' and "y.isTrue ?'class2'"  ></div>

2条回答
Melony?
2楼-- · 2020-04-03 10:17

You can use literal substitutions to achieve this:

<div th:classappend="|${x.isTrue ? 'class1' : ''} ${y.isTrue ? 'class2' : ''}|"></div>

Another method is to simply wrap your conditions with brackets and concatenate them:

<div th:classappend="${(x.isTrue ? 'class1' : '') + (y.isTrue ? ' class2' : '')}"></div>
查看更多
一夜七次
3楼-- · 2020-04-03 10:27

Try this solution. It works very well for me.

<span class="oi" th:classappend="${(h.tipo.label =='Sim/Não' ? 'oi-signpost': '') + 
                                (h.tipo.label =='Quantidade' ? 'oi-target': '')+
                                 (h.tipo.label =='Evitar' ? 'oi-shield': '')}"
                                 title="Visualizar"
                                    aria-hidden="true"></span>
查看更多
登录 后发表回答