Search code examples
javathymeleaf

Create multiple Hyperlinks in single table cell using Thymeleaf


I wish to create a table cell, which would be filled with comma-separated hyperlinks. All should be in one cell. What happens based on the code below is that for each hyperlink a new cell is created... Both code versions below lead to adding cells. Does anyone have an idea on how to do that?

<td th:each="element: ${todo.getAssigneeList()}">
  <span>
    <a th:text="|${element.getId()}, |" th:href="@{/assignee/{id}/detail(id=${element.id})}"></a>
  </span>
</td>

<td th:each="element: ${todo.getAssigneeList()}"><a th:text="|${element.getId()}, |" th:href="@{/assignee/{id}/detail(id=${element.id})}"></a></td>

Below provided solution works as expected. Thank you!


Solution

  • You may combine in your link both iteration and access to the elements. Thymeleaf Attribute Precedence will first parse iteration and only after that will parse the access to the variables ...

    <td><a th:each="element: ${todo.getAssigneeList()}"
        th:text="|${element.getId()}, |"
        th:href="@{/assignee/{id}/detail(id=${element.id})}"></a>
    </td>
    

    Yet another way to use a container tag (for example <th:block>). The code may look like ...

    <td>
        <th:block th:each="element: ${todo.getAssigneeList()}">
            <a th:text="|${element.getId()}, |"
                th:href="@{/assignee/{id}/detail(id=${element.id})}"></a>
        </th:block>
    </td>