[英]How can I get a value of an item from th:each after the page is rendered in order to send it as a request param (without using js)?
I have the following code:我有以下代码:
<tr th:block th:each="participant, iterStat : ${participants}">
<td id="idField" class="table-col-id" th:text="${participant.id}">ex</td>
<td class="table-player-name" th:text="${participant.name}">example</td>
<td class="table-col-operation">
<form id="newTournamentRemovePlayerForm"
th:action="@{/tournament/removePlayer?id={id}(id=${participant.id})}"
method="post">
<button class="player-remove-button table-action-button generic-button"
form="newTournamentRemovePlayerForm"
type="submit">Remove</button>
</form>
</td>
</tr>
My goal is to send a post request to /tournament/removePlayer with the param 'id' equal to the participant.id from the row in which that button is clicked.我的目标是向 /tournament/removePlayer 发送一个 post 请求,参数“id”等于单击该按钮所在行的参与者.id。
The table looks like this:该表如下所示:
||id||name||action||
|1|john|remove|
|2|jane|remove|
|3|jack|remove|
However, in the current state, whichever "remove" button you click, it always sends the lowest id (eg. for the example table, clicking any remove button sends "/tournament/removePlayer?id=1").但是,在当前状态下,无论您单击哪个“删除”按钮,它始终会发送最低的 id(例如,对于示例表,单击任何删除按钮会发送“/tournament/removePlayer?id=1”)。
I checked the Thymeleaf doc: https://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#keeping-iteration-status我检查了 Thymeleaf 文档: https ://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#keeping-iteration-status
And tried using the following:并尝试使用以下内容:
But those would then always send the values indicated in parentheses.但是那些将始终发送括号中指示的值。
Another possibility would be sending directly the value which was filled into the "idField", but I did not manage to find any way to do that in thymeleaf either.另一种可能性是直接发送填充到“idField”中的值,但我也没有设法在百里香叶中找到任何方法来做到这一点。
Is there any way to do what I need without javascript?有没有办法在没有 javascript 的情况下做我需要的事情?
You've messed up with id
attributes.你搞砸了
id
属性。 id
should be unique across the page, but you don't change it while iterating. id
在整个页面上应该是唯一的,但在迭代时不要更改它。 Therefore each form
is identified as "newTournamentRemovePlayerForm"
and each button is assigned to "newTournamentRemovePlayerForm"
form.因此,每个
form
被标识为"newTournamentRemovePlayerForm"
并且每个按钮都被分配给"newTournamentRemovePlayerForm"
表单。 Since there are many of them it fallbacks to the first one.因为有很多,所以它回退到第一个。
You can either fix your ids or even simpler get rid of them:您可以修复您的 ID,甚至更简单地摆脱它们:
<tr th:block th:each="participant, iterStat : ${participants}">
<td class="table-col-id" th:text="${participant.id}">ex</td>
<td class="table-player-name" th:text="${participant.name}">example</td>
<td class="table-col-operation">
<form th:action="@{/tournament/removePlayer?id={id}(id=${participant.id})}"
method="post">
<button class="player-remove-button table-action-button generic-button"
type="submit">Remove
</button>
</form>
</td>
</tr>
Above should work as intended.以上应该按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.