[英]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
元素应用以下步骤:
form
tag form
标签中 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/
) 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.