【问题标题】:Thymeleaf hide certain feature using if statement if the user choose certain option?如果用户选择某个选项,Thymeleaf 使用 if 语句隐藏某些功能?
【发布时间】:2021-05-18 22:23:51
【问题描述】:

我的选择有文档类型、年份和月份,我的文档类型包括月度报告和年度报告

但是如果我的文档类型选择为“年度报告”,我想隐藏“月”选项,因为年度报告是基于年度的。如何将这样的 if 语句放入我的 html 中?

百里香html

            <table class="table table-borderless" cellspacing="0"
                                style="border-inline: 0;">

                                <tbody>
                                    <div th:if="${user!=null}">
                                    <tr>
        <!-- Document Type -->
                                        <td th:text="#{DocType}" />
                                        <td colspan="1">
                                            <div class="dropdown col-sm-6">
                                                <select id="documentType" class="browser-default custom-select"
                                                    th:field="*{type}">
                                                    <option
                                                        th:each="doc:${T(enums.DocumentType).values()}"
                                                        th:value="${doc}" th:text="${doc.label}"></option>
                                                </select>
                                            </div>
                                        </td>
                                    </tr></div>
                                    <tr>
     <!-- YEAR -->
                                        <td th:text="#{Year}" />
                                        <td colspan="1">
                                            <div class="dropdown col-sm-6">
                                                <select class="browser-default custom-select" th:field="*{year}">

                                                    <option value="">Please Select</option>
                                                    <option th:each="year : ${years}" th:value="${year}"
                                                        th:text="${year}"></option>
                                                </select>
                                            </div>
                                        </td>
                                    </tr>
                                    <tr>
     <!-- MONTH -->
                                        <td th:text="#{Month}" />
                                        <td colspan="1">
                                            <div class="dropdown col-sm-6">
                                                <select class="browser-default custom-select" th:field="*{month}">
                                                    <option value="">Please Select</option>

                                                    <option th:each="month : ${months}" th:value="${month}"
                                                        th:text="${month}"></option>
                                                </select>
                                            </div>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>

DocumentType.java

public enum DocumentType {

  MONTHLY("MONTHLY", "Monthly Report"),

  ANNUAL("ANNUAL", "Annual Report"),

  private String code;

  private String label;

  DocumentType(final String code, final String label) {
    this.code = code;
    this.label = label;
  }

  public String getCode() {
    return code;
  }

  public String getLabel() {
    return label;
  }
}

【问题讨论】:

  • 如果对你有帮助,你可以接受我的回答

标签: java html thymeleaf


【解决方案1】:

如果选择的文档是每年,要隐藏月份选择,您可以在每月使用以下条件。

th:if="${DocumentType.getCode() != 'ANNUAL'}"

所以代码应该是这样的

<tr th:if="${DocumentType.getCode() != 'ANNUAL'}">
    <!-- MONTH -->
    <td th:text="#{Month}" />
    <td colspan="1">
        <div class="dropdown col-sm-6">
            <select class="browser-default custom-select" th:field="*{month}">
                <option value="">Please Select</option>
                <option th:each="month : ${months}" 
                        th:value="${month}"
                        th:text="${month}"></option>
            </select>
        </div>
    </td>
</tr>

要了解如何在 Thymeleaf 中访问枚举,请使用此链接 Comparing the enum constants in thymeleaf

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-23
    • 1970-01-01
    • 2013-09-30
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    相关资源
    最近更新 更多