Thymeleaf classappend for multiple classes

▼魔方 西西 提交于 2020-01-30 04:31:17

问题


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>

回答1:


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>



回答2:


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>


来源:https://stackoverflow.com/questions/41480973/thymeleaf-classappend-for-multiple-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!