0

Assuming I have

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

Sample Data:

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

I am trying to create a table with thymeleaf that would have three columns Event Date, Event Owner, event Data. Such that

  1. event date would have row span of size equals to the number of event Data on that date.
  2. event owner would have a row span of size equals to the number of event data on that date for that event owner
  3. event data

For the data above, the table would like

enter image description here

I want to create a table that would display the list of eventData, grouped by the eventOwner, grouped by the occuranceDate

I am passing Map<Date, Map<String, List<String>>> to Thymeleaf (I am open to change data structure). I would appreciate any help in how to generate the table , it is proving to be very complicated.

I tried following : Thymeleaf Table issues with rowspan (1 Order, N Articles) but that becomes complicated with three different columns that are representing nested maps and lists

Wael Awada
  • 1,506
  • 3
  • 18
  • 31

1 Answers1

1

You can do this with just a List<Event>. (I did add a variable occuranceDateString which is just a string version of 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>
Metroids
  • 18,999
  • 4
  • 41
  • 52
  • I added a small bug fix... `newOwner=${(stat.index == 0)...` was changed to `newOwner=${newDate || (stat.index == 0)...` (if a new date started with the same owner as the last row) – Metroids Jan 29 '22 at 18:14