Datatables添加新增数据和修改修改弹框,并与java后端实现数据交互的完整html示例
时间: 2024-03-08 07:49:59 浏览: 217
以下是一个完整的HTML示例代码,演示如何使用Datatables添加新增数据和修改数据弹框,并与Java后端实现数据交互:
```
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Datatables示例</title>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.25/css/jquery.dataTables.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.7.1/css/buttons.dataTables.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/select/1.3.3/css/select.dataTables.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/editor/2.0.6/css/editor.dataTables.min.css">
</head>
<body>
<table id="example" class="display" style="width:100%">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
</tbody>
<tfoot>
<tr>
<th>ID</th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Salary</th>
</tr>
</tfoot>
</table>
<button id="add">Add new record</button>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.7.1/js/dataTables.buttons.min.js"></script>
<script src="https://cdn.datatables.net/select/1.3.3/js/dataTables.select.min.js"></script>
<script src="https://cdn.datatables.net/editor/2.0.6/js/dataTables.editor.min.js"></script>
<script>
$(document).ready(function() {
var table = $('#example').DataTable({
ajax: {
url: 'data.json',
dataSrc: ''
},
columns: [
{ data: 'id' },
{ data: 'name' },
{ data: 'position' },
{ data: 'office' },
{ data: 'salary' }
]
});
var editor = new $.fn.dataTable.Editor({
ajax: {
create: {
url: '/add',
type: 'POST'
},
edit: {
url: '/update',
type: 'PUT'
},
remove: {
url: '/delete',
type: 'DELETE'
}
},
table: '#example',
fields: [
{ label: 'ID:', name: 'id' },
{ label: 'Name:', name: 'name' },
{ label: 'Position:', name: 'position' },
{ label: 'Office:', name: 'office' },
{ label: 'Salary:', name: 'salary' }
]
});
$('#add').on('click', function () {
editor.create({
title: 'Add new record',
buttons: 'Add'
});
});
$('#example').on('click', 'tbody td:not(:first-child)', function (e) {
editor.inline(this);
});
});
</script>
</body>
</html>
```
在这个示例中,我们使用了Datatables的Editor插件来实现添加/修改数据和删除数据的功能。在Java后端,我们使用了Spring MVC框架来处理提交的数据。需要注意的是,示例中的Java后端代码并不完整,需要根据实际情况进行修改。
阅读全文