1

I'm having trouble putting a button into the element of the list based on Thymeleaf. I would like to have a button next to element of the list.

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorate="~{layout.html}">
<head>
    <meta charset="UTF-8">
    <title>Home</title>
</head>
<body>
<section layout:fragment="" content="">
    <div class="row">
        <div class="col-2">
            <a th:href="@{/archives}">archiwum zadań</a>
        </div>
        <div class="col-2">
            <a th:href="@{/add}">dodawanie zadania</a>
        </div>
    </div>

    <ul>
        <li th:each="task: ${tasks}"
            th:text="|${task.getId()} ${task.getName()} ${task.getCategory().getDescription()} ${task.isFinished()}|">
            <input type="submit" value="Done">
        </li>
    </ul>
</section>
</body>
</html>

1 Answer 1

2

Thymeleaf will replace any pre-existing tag content with whatever is generated by the th:text expression.

In your case that pre-existing content is your <input> element - which is why the buttons are not displayed.

One way to avoid this is to place your th:text into a span inside the <li>:

<ul>
    <li th:each="task: ${tasks}">
        <span th:text="|${task.id} ${task.name} ... |"></span>
        <input type="submit" value="Done">
    </li>
</ul>

(I removed a couple of your fields from my example, for brevity).

Note also in my case, I have changed ${task.getId} to ${task.id}. As long as you have appropriately named getters (as per the JavaBeans naming convention) you can use the field name - and Thymeleaf will find the correct getter to call.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.