简体   繁体   English

如何在页面呈现后从 th:each 获取项目的值,以便将其作为请求参数发送(不使用 js)?

[英]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:并尝试使用以下内容:

  • iterStat.current.id (sends "1"), iterStat.current.id(发送“1”),
  • iterStat.index (sends "0"), iterStat.index(发送“0”),
  • and iterStat.count (sends "1)和 iterStat.count(发送“1)

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.

相关问题 如何使用AsyncTask(通过POST请求)将param值发送到Server并获取数据? - How to send param value to Server and get data using AsyncTask (by POST Request)? JSF 2 - 如何使用JSF EL从web.xml获取上下文参数值? - JSF 2 - How can I get a context-param value from web.xml using JSF EL? 如何在 Spring 启动中的获取映射请求中发送一个字符串列表作为参数,其中包含很多项目? - How can I send a String list as param with a lot of items in a get mapping request in Spring boot? 如何使用 firebase 获取 listView 中每个项目的数据? - How can I get the data for each item in the listView using firebase? 如何使用此格式JAVA发送GET请求 - How can I send a GET request using this format JAVA 如何从URL获取渲染的网页页面? - How do I get the rendered web page page from a URL? 客户下订单后,如何让谷歌结帐重定向到项目特定页面? - How do I get google checkout to redirect to item specific page after customer placed order? 如何在没有表单的情况下为每个循环从一个JSP页面向另一个页面发送值? - How to send a value from one JSP page to another for each loop without a form? 如何使用 terminate_after 参数通过 ElasticsearchRestTemplate 向弹性发送搜索请求? - How to send search request to elastic by ElasticsearchRestTemplate with terminate_after param? 如何从php页面(没有json)获取数据? - How can I get data from php page (without json)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM