简体   繁体   中英

Thymeleaf: th:each for two table rows?

How can I, inside a th:each loop create a group of two rows instead of just one?

I know I can do:

<tr th:each="obj: ${listOfObjects}">
   <td>a column with data: ${obj.id}</td>
</tr>

However, I want two <tr> elements to be created, as I would do with JSTL:

<c:forEach items="${listOfObjects}" var="obj">
    <tr>
       <td>${obj.id}</td>
    </tr>
    <tr>
       <td>${obj.name}</td>
    </tr>
</c:forEach>

Is there a way to achieve that with Thymeleaf?

You could use a th:block element for grouping the rows together and repeat them:

<th:block th:each="obj: ${listOfObjects}">
    <tr>
       <td th:text="${obj.id}"></td>
    </tr>
    <tr>
       <td th:text="${obj.name}"></td>
    </tr>
</th:block>

You can read more about th:block here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM