Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymeleaf classappend for multiple classes

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>
like image 773
Vazgen Torosyan Avatar asked Jan 05 '17 09:01

Vazgen Torosyan


2 Answers

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>
like image 191
Edd Avatar answered Sep 21 '22 16:09

Edd


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>
like image 23
Michel Fernandes Avatar answered Sep 22 '22 16:09

Michel Fernandes