【问题标题】:Multidimensional list in Thymeleaf (Java) - List<List<Object>>Thymeleaf (Java) 中的多维列表 - List<List<Object>>
【发布时间】:2017-07-26 20:13:33
【问题描述】:

你们有什么建议吗,如何在 Thymeleaf 中遍历一个多维列表?

我的多维列表如下所示:

@Override
public List<List<PreferredZone>> findZonesByPosition(List<Position> positionList) {

        List <PreferredZone> prefZone = new ArrayList<>();
        List<List<PreferredZone>> listPrefZone = new ArrayList<>();
        long positionId = 0;

        for (int i = 0; i < positionList.size(); i++) {

            positionId = positionList.get(i).getPositionId();
            prefZone = prefZoneDAO.findFilteredZone(positionId);

            listPrefZone.add(prefZone);
        }

    return listPrefZone;
}

在我的控制器中作为属性:

List<List<PreferredZone>> prefZoneList = prefZoneService.findZonesByPosition(positionList);
    model.addAllAttributes(prefZoneList);

最后我尝试在 HTML 表格中迭代这个二维列表:

<table th:each="prefList :#{prefZoneList}" class="table table-striped display hover">
  <thead>
    <tr>
      <th>ISO</th>
      <th>Name</th>
      <th>Ausschluss</th>
    </tr>
  </thead>
  <!-- Loop für die Daten -->
 
  <tr th:each="row, iterState :${prefList}" class="clickable-row">
    <td th:text="${row[__${iterState.index}__]}.zoneIso"></td>
    <td th:text="${row[__${iterState.index}__]}.zoneName"></td>
    <td style="text-align:center;">
      <input type="checkbox" th:value="${${row[__${iterState.index}__]}.zoneId}" id="zone" class="checkbox-round" />
    </td>
  </tr>
</table>

但它不起作用。我不知道如何解决这个问题。 我必须有一个多维列表,因为我有一个包含多条记录的表,并且每条记录都包含一个用于打开模式窗口的按钮。每个窗口都包含一个 HTML 表,我必须在其中显示记录。

你对我有什么建议吗?

【问题讨论】:

  • “它不起作用”是什么意思?如果将 ${row[__${iterState.index}__]}.zoneIso 更改为 ${row.zoneIso} 会发生什么?
  • 当我按照你提到的那样做时,我得到一个跟随错误:org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'zoneIso' cannot be found on object of输入“java.lang.String”
  • 当我尝试使用: 呈现的 HTML 如下所示:??prefZoneList_de ??

标签: list multidimensional-array thymeleaf


【解决方案1】:

您在使用#{prefZoneList} 和使用iterState.index 时(如cmets 中所述)有一个错误

试试看:

    <table th:each="prefList : ${prefZoneList}" class="table table-striped display hover">
      <thead>
        <tr>
          <th>ISO</th>
          <th>Name</th>
          <th>Ausschluss</th>
        </tr>
      </thead>

      <tr th:each="row : ${prefList}" class="clickable-row">
        <td th:text="${row.zoneIso}"></td>
        <td th:text="${row.zoneName}"></td>
        <td style="text-align:center;">
          <input type="checkbox" th:value="${row.zoneId}" id="zone" class="checkbox-round" />
        </td>
      </tr>
    </table>

语法#{...} - 消息表达式

iterState.index 是当前迭代索引,从 0 开始,使用 like ${prefList[__${iterState.index}__].element} where element - 在prefList 中归档。

【讨论】:

  • 伊丽莎白,感谢您的及时回复!老实说,我没有验证您的建议,因为我用 JSON 解决了​​我的挑战,无论如何,在我的情况下,什么是更好的解决方案。然而,我与几个朋友交谈,他们证实了你的回答。我认为它会起作用。
猜你喜欢
  • 2021-12-15
  • 1970-01-01
  • 2014-03-13
  • 2018-07-17
  • 1970-01-01
  • 2021-08-08
  • 2019-12-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多