简体   繁体   English

带有复选框的SpringBoot + Thymeleaf动态表与Controller通信

[英]SpringBoot + Thymeleaf dinamic table with checkbox in row communicate with Controller

Maybe the title is more confusing than the problem it self. 标题可能比本身的问题更令人困惑。 Here's my situation, with Thymeleaf I show a table with a list of clients. 这是我的情况,在Thymeleaf中,我显示了一个包含客户列表的表。 It shows Name, LastName, a button to delete it and a checkbox to display it's state (if the user is active or inactive) 它显示名称,姓氏,一个将其删除的按钮以及一个显示其状态的复选框(如果用户处于活动状态或非活动状态)

<table class="table">
<thead>
<tr>
    <th>Name</th>
    <th>Last Name</th>
</tr>
</thead>
<tbody>
<tr th:each="client : ${clientList}">
    <td th:text="${client.name}"></td>
    <td th:text="${client.lastName}"></td>
    <td>
        <a th:href="@{/delete_client/} + ${client.id}" class="btn btn-danger">Delete</a>
    </td>
    <td>
        <input type="checkbox" name="my-checkbox" th:checked="${client.state} ? 'checked'">
    </td>

</tr>
</tbody>

So far, no problem. 到目前为止,没有问题。 But what I don't know how to do is communcate the controller when the user click the checkboxes, in order to disable or enable that particular client. 但是我不知道该怎么做,就是在用户单击复选框时与控制器通信,以禁用或启用该特定客户端。

Something like what the button does to delete the client, but to change it's state. 类似于该按钮所做的操作可删除客户端,但可更改其状态。

I was told to use AJAX, but don't know how. 有人告诉我使用AJAX,但不知道如何使用。

Thank you! 谢谢!

You can avoid ajax. 您可以避免Ajax。 For deleting-alike effect you can apply following steps to the td element containing a checkbox: 对于类似删除的效果,可以对包含复选框的td元素应用以下步骤:

  • wrap up the checkbox in the form tag 将复选框包装在form标签中
  • provide the proper action attribute in the form . form提供适当的action属性。 The action is an endpoint which handles updating of a client's state (similarily to /delete_client/ ) 该操作是处理客户端状态更新的端点( /delete_client/
  • provide onclick="submit();" 提供onclick="submit();" attribute in the checkbox, which will trigger mentioned endpoint when the user clicks it 复选框中的属性,当用户单击它时将触发提到的端点

Eg 例如

<td>
    <form th:action="@{/switch_client_state/} + ${client.id}">
        <input type="checkbox" name="my-checkbox" onclick="submit();" th:checked="${client.state} ? 'checked'">
    </form>
</td>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM