O
O
Orkhan Hasanli2018-12-05 02:38:09
Java
Orkhan Hasanli, 2018-12-05 02:38:09

How to pass variable value (id) to modal window in thymeleaf?

Hello!
In the thymeleaf template, I display a loop with data.
For example, a code snippet:

......
<tr th:each="user: ${user}">
   <td th:text="${user.userId}">User ID</td>
   <td>
    <button type="button" class="btn btn-warning" data-toggle="modal" data-target="#sendData">Отправить</button>
   </td>
   ......
</tr>
......

When you click on the button, a modal with a form opens. Accordingly, here is a code snippet:
<form th:action="@{'/users/' + ${user.userId}}" method="post">
    <button type="submit" class="btn btn-warning float-right" data-dismiss="modal">Отправить</button>
</form>

Since this is outside the loop, it cannot get user.userId. How to pass the userId from the object on which the click was made to open the modal to the modal window?
Thank you in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-12-05
@azerphoenix

First, Thymeleaf doesn't have modal windows. Your modal is from Bootstrap. Second, it's important to understand that Thymeleaf is a templating engine, it runs on the backend, while Bootstrap runs on the frontend. Backend and frontend are two different programs written in different languages ​​and running on different computers at different times. So you either have to spawn different modals on each iteration in the template engine loop, or you have to write javascript code that will pass data from the pressed button to a single modal window. Naturally, the second option makes more sense.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question