html - 数据表:在客户端删除行
问题描述
我有一个使用数据表的 SpringBoot 应用程序,这里是模板上的数据表
<table id="example" class="display" style="width:100%">
<thead>
<tr>
<th>Position</th>
<th>Actions1</th>
<th>Actions2</th>
</tr>
</thead>
<tbody>
<tbody>
<tr th:each="pic: ${pics}" >
<td class="col_name" >
<div class="box small">
<img th:src="${pic}"/>
</div>
</td>
<td class="col_actions">
<a style="color:#808080; margin-right: 10px;">
<input type="radio" name="${pic}" th:field="*{nadal}" th:value="${pic}" >Nadal<br>
</a>
</td>
<td><button>Delete</button></td>
</tr>
</tbody>
<tfoot>
<tr>
<th>Position</th>
<th>Actions1</th>
<th>Actions2</th>
</tr>
</tfoot>
</table>
和同一页面上的javascript代码:
<script type="text/javascript">
$(document).ready(function() {
$('#example').DataTable({
searching: false,
paging: false
}).on("click", "button", function(){
alert('deleting row');
console.log($(this).parent());
table.row($(this).parents('tr')).remove().draw(false);
});
});
</script>
但是当我点击它时,它会提交表单的服务器
解决方案
尝试这个:
<script type="text/javascript">
$(document).ready(function() {
const table = $('#example').DataTable({
searching: false,
paging: false
}).on("click", "button", function () {
alert('deleting row');
console.log($(this).parent());
table.row($(this).parents('tr')).remove().draw(false);
});
});
</script>
推荐阅读
- django - 如何在 Django Rest Framework 中创建具有嵌套对象的对象
- php - 获取一年的记录数
- java - 从列表中设置值时,如何在电子表格控件中的单元格内旋转值?
- wordpress - 如何在自定义帖子类别页面模板上设置 post_per_page?
- sql-server - SQL CTE vs Temp 表在性能方面
- kubernetes - awx-db pod 处于挂起模式
- c# - 具有无方案 URI 的脚本标记在发布时生成文件未找到错误
- python - 如何在优化代码中将约束重新编写为正半定?
- python - 如何在没有 .readlines() 的情况下读取 .txt 文件/用 \n 替换 UTF-8 换行符?
- android - Firebase - 如何使用 Facebook 登录执行 Robo 测试