Thymeleaf: how to iterate over list, while using iteration variable in table row attribute

18 views Asked by At

I am using Thymeleaf inside a Spring Boot application.

I have a Thymeleaf list variable "users_online", I can produce a table like so:

    <tbody id="users-online-list">
       <tr th:each="username: ${users_online}">
           <td th:text="${username}"></td>
       </tr>
    </tbody>

That works fine. However, what I want to do is add an id attribute in the tr element. For example, if the username is user1, this is what I would like for the row:

<tr id="online-user1">
  <td>user1</td>
</tr>

I have tried several variations of iterating like this one that doesn't work:

<tbody id="users-online-list">
   <tr th:each="username: ${users_online}"
       th:id="online-${username}">
       <td th:text="${username}"></td>
   </tr>
</tbody>

I have also tried to place the iteration inside the tbody element instead of the tr element, but that doesn't work either. Any suggestions?

Thanks

0

There are 0 answers