【问题标题】:Thymeleaf iterate map of maps to create table with rowspansThymeleaf 迭代地图以创建具有行跨度的表
【发布时间】:2022-01-29 09:13:51
【问题描述】:

假设我有

Date occuranceDate; //Dates only have years, months, days 
String eventOwner;
String eventData; 

样本数据:

12-01-2021 , John Doe, random event data 1
12-01-2021, Jane Doe, random event data 2
12-01-2021, Jane Doe, random event data 3
12-02-2021, John Doe, random event data 4

我正在尝试使用 thymeleaf 创建一个包含三列 Event Date, Event Owner, event Data 的表。这样的

  1. 事件日期的行跨度大小等于该日期的事件数据数。
  2. 事件所有者的行跨度将等于该事件所有者在该日期的事件数据数
  3. 事件数据

对于上面的数据,表格想

我想创建一个表格来显示eventData 的列表,按eventOwner 分组,按occuranceDate 分组

我将 Map<Date, Map<String, List<String>>> 传递给 Thymeleaf(我愿意更改数据结构)。对于如何生成表格,我将不胜感激,事实证明它非常复杂。

我尝试了以下操作:Thymeleaf Table issues with rowspan (1 Order, N Articles),但这会变得复杂,因为三个不同的列代表嵌套的地图和列表

【问题讨论】:

    标签: spring-boot thymeleaf freemarker spring-thymeleaf


    【解决方案1】:

    您只需 List<Event> 即可完成此操作。 (我确实添加了一个变量occuranceDateString,它只是occuranceDate 的字符串版本。)

    <table>
      <tr   th:each="event, stat: ${events}"
            th:with="
              newDate=${(stat.index == 0) || (events[stat.index - 1].occuranceDateString != event.occuranceDateString)},
              newOwner=${newDate || (stat.index == 0) || (events[stat.index - 1].eventOwner != event.eventOwner)},
            " valign="top">
        <td th:if="${newDate}" th:text="${event.occuranceDateString}" th:rowspan="${events.?[occuranceDateString == #root.event.occuranceDateString].size()}" />
        <td th:if="${newOwner}" th:text="${event.eventOwner}" th:rowspan="${events.?[occuranceDateString == #root.event.occuranceDateString].?[eventOwner == #root.event.eventOwner].size()}" />
        <td th:text="${event.eventData}" />
      </tr>      
    </table>
    

    【讨论】:

    • 我添加了一个小错误修复...newOwner=${(stat.index == 0)... 已更改为 newOwner=${newDate || (stat.index == 0)...(如果新日期与最后一行的所有者相同)
    猜你喜欢
    • 2019-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-03
    • 2015-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多