简体   繁体   English

用thymeleaf添加条件属性

[英]Add conditional attribute with thymeleaf

I have a thymeleaf fragment to create input fields like: 我有一个百万美元片段来创建输入字段,如:

<div th:fragment="formField">
        <input th:type="${type}" th:errorclass="field_error" th:field="*{__${field}__}" th:placeholder="#{__${placeholder}__}" />
</div>

This fragment is eg used like: 该片段例如使用如下:

<div th:replace="fragments :: formField (type='password', field='password', placeholder='resetPassword.form.password')">

Now the autofocus attribute should be added or not to the input field based on the parameters of the fragment. 现在,应根据片段的参数将自动聚焦属性添加到输入字段中。 Using the fragment eg like this should add the autofocus attribute: 使用像这样的片段应该添加自动聚焦属性:

<div th:replace="fragments :: formField (type='password', field='password', placeholder='resetPassword.form.password', autofocus='autofocus')">

I could not find a way to add the autofocus attribute conditionally to the input tag based on the fragment parameters. 我找不到根据片段参数有条件地将自动聚焦属性添加到输入标签的方法。 I tried using th:attr but always ended up in syntax errors. 我尝试使用th:attr但总是出现语法错误。

Is there a way to create html attributes conditionally with thymeleaf? 有没有办法用百里香叶有条件地创建html属性?

I guess the problem is that if you declare the additional parameter in the fragment - you need to pass it. 我想问题是如果你在片段中声明了附加参数 - 你需要传递它。 Thus, you could pass either autofocus , or empty value ('') and process the check with Thymeleaf. 因此,您可以传递自动对焦或空值('')并使用Thymeleaf处理检查。

For instance, you call: 例如,你打电话:

<div th:replace="fragments :: formField (type='password', field='password', 
     placeholder='resetPassword.form.password', autofocus='')">

Then process it with: 然后处理它:

<div th:fragment="formField">
    <input th:if="${!autofocus.isEmpty()}" th:type="${type}"
           th:errorclass="field_error" th:field="*{__${field}__}"
           th:placeholder="#{__${placeholder}__}" autofocus="true"/>
    <input th:if="${autofocus.isEmpty()}" th:type="${type}"
           th:errorclass="field_error" th:field="*{__${field}__}"
           th:placeholder="#{__${placeholder}__}"/>
</div>

Or: 要么:

<div th:fragment="formField" th:switch="${autofocus}">
    <input th:case="'autofocus'" th:type="${type}"
           th:errorclass="field_error" th:field="*{__${field}__}"
           th:placeholder="#{__${placeholder}__}" autofocus="true"/>
    <input th:case="*" th:type="${type}" th:errorclass="field_error"
           th:field="*{__${field}__}"
           th:placeholder="#{__${placeholder}__}"/>
</div>

But I guess James comment to use th:autofocus would be the best solution: 但我想詹姆斯评论使用th:自动对焦将是最好的解决方案:

<div th:fragment="formField">
        <input th:type="${type}" th:errorclass="field_error" 
               th:field="*{__${field}__}" th:placeholder="#{__${placeholder}__}" 
               th:autofocus="${!autofocus.isEmpty()}" />
</div>

In all the cases you still need to pass autofocus="autofocus" or autofocus="" as a parameter. 在所有情况下,您仍然需要将autofocus =“autofocus”autofocus =“”作为参数传递。

I think it's a too complex task for an fragment. 我认为片段的任务太复杂了。 I solved such problem with a Dialect. 我用方言解决了这个问题。 See my question and the solution . 看到我的问题解决方案

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM