简体   繁体   English

如何根据百里香叶中的条件将样式应用于 div?

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

I have a <div> block which I need to set to display:none or display:block based on the condition.我有一个<div>块,我需要根据条件将其设置为display:nonedisplay:block The html looks like this, html看起来像这样,

<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.抛出org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression:错误消息。

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.我可以做th:classappend来设置一些类并完成这项工作,但想知道 elvis/三元运算符是否支持 thymeleaf th:style标签。

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.样式设置为display:block并且对于其他角色不显示块。

在简单的情况下,它可以写成

<div th:style="${filed==null} ? 'opacity:.3'">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 百里香叶表td中的条件如何申请 - How to apply if condition in table td in thymeleaf 如何基于百里香中的其他字段进行计算? - How can I do calculation based on other fields in thymeleaf? Thymeleaf-如何根据互斥条件应用两种(或更多)样式 - Thymeleaf - How to apply two (or more) styles based on mutually exclusive conditions 如何从 thymeleaf 中的 REST 样式 URL 获取请求参数? - How can I get request parameters from REST style URL in thymeleaf? 如何根据 controller 条件隐藏/显示 thymeleaf 字段? - How to hide/show thymeleaf fields based on controller condition? Thymeleaf如何根据另一个表中的值显示一个表中的列 - Thymeleaf How can I display a column from one table based on a value from another table Spring Boot应用程序-如何基于当前对象将数据(列表中的列表)从控制器传递到Thymeleaf? - Spring Boot Application - How can I pass data (list in list) from controller to Thymeleaf based on current object? 在Thymeleaf中,如何基于当前模型对象为HTML选择器选择th:selected值? - In Thymeleaf, how can I choose the th:selected value for an HTML selector based on the current model object? 我尝试用OR在百里香中写一个条件 - I try to write a condition in thymeleaf with OR 如何在 Thymeleaf 中应用 elvis 运算符? - How to apply elvis operator in Thymeleaf?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM