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
- event date would have row span of size equals to the number of event Data on that date.
- event owner would have a row span of size equals to the number of event data on that date for that event owner
- event data
For the data above, the table would like
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
CodePudding user response:
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>

