添加表格动态增删改查示例

This commit is contained in:
RuoYi
2019-06-18 21:56:08 +08:00
parent 1c235dc7f6
commit 7d6cdabc09
8 changed files with 282 additions and 24 deletions

View File

@ -3,7 +3,75 @@
<head>
<th:block th:include="include :: header('其他操作')" />
</head>
<body class="gray-bg">
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
<form class="form-horizontal" id="form-demo-1">
<div class="form-group">
<label class="col-sm-2 control-label">用户名称:</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="userName"placeholder="请输入用户名称">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">手机号码:</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="phonenumber" maxlength="11"placeholder="请输入手机号码">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="button" class="btn btn-sm btn-primary" onclick="submit1()"><i class="fa fa-check"></i>保 存(不刷新当前页)</button>&nbsp;
</div>
</div>
</form>
<hr/>
<form class="form-horizontal" id="form-demo-2">
<div class="form-group">
<label class="col-sm-2 control-label">用户名称:</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="userName"placeholder="请输入用户名称">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">手机号码:</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="phonenumber" maxlength="11"placeholder="请输入手机号码">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="button" class="btn btn-sm btn-primary" onclick="submit2()"><i class="fa fa-check"></i>保 存(刷新当前页)</button>&nbsp;
</div>
</div>
</form>
</div>
<th:block th:include="include :: footer" />
<script type="text/javascript">
var prefix = ctx + "demo/operate";
function submit1(){
$.operate.saveModal(prefix + "/edit", $('#form-demo-1').serialize());
}
function submit2(){
$.ajax({
url: prefix + "/edit",
data: $('#form-demo-2').serialize(),
type: "post",
success: function(result) {
if (result.code == 0) {
layer.msg("保存成功,正在刷新数据请稍后……", {
icon: 1,
time: 500,
shade: [0.1, '#8F8F8F']
},function() {
location.reload();
});
} else {
alert(result.msg);
}
}
})
}
</script>
</body>
</html>

View File

@ -0,0 +1,178 @@
<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
<th:block th:include="include :: header('动态增删改查')" />
</head>
<body class="gray-bg">
<div class="container-div">
<div class="btn-group-sm" id="toolbar" role="group">
<a class="btn btn-success" onclick="insertRow()">
<i class="fa fa-plus"></i> 新增行
</a>
<a class="btn btn-danger btn-del disabled" onclick="removeRow()">
<i class="fa fa-remove"></i> 删除选择行
</a>
<a class="btn btn-danger" onclick="removeRowByUniqueId()">
<i class="fa fa-remove"></i> 根据值删除行
</a>
<a class="btn btn-danger" onclick="removeRowAll()">
<i class="fa fa-remove"></i> 删除所有行
</a>
<a class="btn btn-info" onclick="updateRow()">
<i class="fa fa-edit"></i> 修改行
</a>
<a class="btn btn-info" onclick="updateRowByUniqueId()">
<i class="fa fa-edit"></i> 根据值修改行
</a>
<a class="btn btn-primary" onclick="getSelections()">
<i class="fa fa-search"></i> 查询选择数据
</a>
<a class="btn btn-info" onclick="getRowByUniqueId()">
<i class="fa fa-edit"></i> 根据值查询行
</a>
<a class="btn btn-primary" onclick="getData()">
<i class="fa fa-search"></i> 查询所有数据
</a>
</div>
<div class="row">
<div class="col-sm-12 select-table table-striped">
<table id="bootstrap-table" data-mobile-responsive="true"></table>
</div>
</div>
</div>
<div th:include="include :: footer"></div>
<script th:inline="javascript">
var prefix = ctx + "demo/table";
$(function() {
var options = {
url: prefix + "/list",
showSearch: false,
showRefresh: false,
showToggle: false,
showColumns: false,
pagination: false,
uniqueId: "userId",
height: 400,
columns: [{
checkbox: true
},
{
field : 'userId',
title : '用户ID'
},
{
field : 'userCode',
title : '用户编号'
},
{
field : 'userName',
title : '用户姓名'
},
{
field : 'userPhone',
title : '用户手机'
},
{
field : 'userEmail',
title : '用户邮箱'
},
{
field : 'userBalance',
title : '用户余额'
}]
};
$.table.init(options);
});
/* 新增表格行 */
function insertRow(){
var randomId = 100 + ~~(Math.random() * 100)
$.btTable.bootstrapTable('insertRow', {
index: 0, // 你想插入到哪0表示第一行
row: {
userId: randomId,
userCode: 2000000 + randomId,
userName: '测试' + randomId,
userPhone: '1588888888',
userEmail: 'ry1@qq.com',
userBalance: 10 + randomId,
}
})
}
/* 删除指定表格行 */
function removeRow(){
var ids = $.table.selectColumns("userId");
if (ids.length == 0) {
$.modal.alertWarning("请至少选择一条记录");
return;
}
$.btTable.bootstrapTable('remove', {
field: 'userId',
values: ids
})
}
/* 删除行ID值为1的数据 */
function removeRowByUniqueId(){
$.btTable.bootstrapTable('removeByUniqueId', 1)
}
/* 删除所有表格行 */
function removeRowAll(){
$.btTable.bootstrapTable('removeAll')
}
/* 修改表格行 */
function updateRow(){
var randomId = 100 + ~~(Math.random() * 100)
$.btTable.bootstrapTable('updateRow', {
index: 0, // 你想修改哪行0表示第一行
row: {
userId: randomId,
userCode: 3000000 + randomId,
userName: '测试' + randomId,
userPhone: '1599999999',
userEmail: 'ry2@qq.com',
userBalance: 50 + randomId,
}
})
}
/* 修改行ID值为1的数据 */
function updateRowByUniqueId(){
var randomId = 100 + ~~(Math.random() * 100)
$.btTable.bootstrapTable('updateByUniqueId', {
id: 1,
row: {
userId: randomId,
userCode: 3000000 + randomId,
userName: '测试' + randomId,
userPhone: '1599999999',
userEmail: 'ry2@qq.com',
userBalance: 50 + randomId,
}
})
}
/* 查询表格所有数据值 */
function getData(){
var data = $.btTable.bootstrapTable('getData');
alert(JSON.stringify(data))
}
/* 查询行ID值为1的数据 */
function getRowByUniqueId(){
var data = $.btTable.bootstrapTable('getRowByUniqueId', 1);
alert(JSON.stringify(data))
}
/* 查询表格选择行数据值 */
function getSelections(){
var data = $.btTable.bootstrapTable('getSelections');
alert(JSON.stringify(data))
}
</script>
</body>
</html>

View File

@ -104,6 +104,7 @@
<li><a class="menuItem" th:href="@{/demo/table/event}">自定义触发事件</a></li>
<li><a class="menuItem" th:href="@{/demo/table/detail}">表格细节视图</a></li>
<li><a class="menuItem" th:href="@{/demo/table/image}">表格图片预览</a></li>
<li><a class="menuItem" th:href="@{/demo/table/curd}">动态增删改查</a></li>
<li><a class="menuItem" th:href="@{/demo/table/other}">表格其他操作</a></li>
</ul>
</li>