I am tryring to create a table from a list of elements with Thymeleaf and Twitter Bootstrap.
The table rows should each be exapandable by click.
I am trying to do this with the standard bootstrap mechanism with ‘data-toogle’ and ‘accordion’.
The problem is that I cannot get Thymeleaf to print two rows of each entry from the list. The best I could archive is the following, with the problem that opening bodies are under the original table.
<table class="table table-striped table-hover">
<thead>
<th>Name</th>
<th>Value</th>
</thead>
<tbody>
<tr th:each="result: ${results}" th:attr="data-target='#accordion_'+${result.name}" data-toggle="collapse" class="accordion-toggle">
<td th:text="${result.name}"></td>
<td th:text="${result.value}"></td>
</tr>
<tr th:each="result: ${results}">
<td colspan="6" class="hiddenRow">
<div class="accordion-body collapse" th:id="'#accordion_'+${result.name}">there is nothing to see here (yet)
</div>
</td>
</tr>
</tbody>
</table>
So there are different questions/ answer in here:
- Is there a way in Thymeleaf to print two rows per list item?
- Is there a better mechanism to have collapsable list items with Thymeleaf?
2
Answers
you can print two rows in Thymeleaf by using the
th:block
tag like this (not tested)The solution didn’t work for me until I removed the # from the id attribute, so the data targets
<div>
now looks like this: