How can I apply style to a div based on condition in thymeleaf?

左心房为你撑大大i 提交于 2019-12-01 15:05:56

问题


I have a <div> block which I need to set to display:none or display:block based on the condition. The html looks like this,

<div style="display:none;"> 
    //some html block content
</div>

I've tried the following code in thymeleaf,

<div th:style="${condition} == 'MATCH' ? display:block : display:none"> 
    //some html block content
</div>

But the above expression is not working. throws org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: error message.

I can do th:classappend to set some class and make this work but want to know if elvis/ternary operator will support on thymeleaf th:style tag.


回答1:


Solved it while posting the question,

th:style="${condition ? 'display:block' : 'display:none'}" >

would produce the necessary conditional style. If condition is true display is set to block and none if condition is false.

For admin,

th:style="${role == 'ADMIN' ? 'display:block' : 'display:none'}" >

the style is set to display:block and for other roles the block is not displayed.



来源:https://stackoverflow.com/questions/39165486/how-can-i-apply-style-to-a-div-based-on-condition-in-thymeleaf

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