layui的数据表格+springmvc实现增加数据功能:

可以看看:

layui的数据表格+springmvc实现数据显示,分页功能
<https://blog.csdn.net/weixin_39220472/article/details/80376468>

layui的数据表格+springmvc实现搜索功能
<https://blog.csdn.net/weixin_39220472/article/details/80659433>


js中要加入:使隐藏的增加数据的form表单显示出来


add: function(){ layer.open({ title:'增加管理员', type:1, area:['400px','400px'],
content:$("#form") }); }
jsp页面:

表示增加的图标:


<script type="text/html" id="toolbarDemo"> <i class="layui-icon" data-type=
"add"></i> <i class="layui-icon" data-type="delete"></i>
</script>表示form表单:


<%--增加的弹框--%> <form class="layui-form layui-form-pane1" id="form" name="form"
style="display: none;" action="<%=request.getContextPath()%>/admin/addAdmin"
method="post" lay-filter="first"> <div class="layui-form-item"> <label class=
"layui-form-label">账号</label> <div class="layui-input-inline"> <input type=
"text"name="account" lay-verify="required|title" required placeholder="请输入账号"
autocomplete="off" class="layui-input"> </div> </div> <div class=
"layui-form-item"> <label class="layui-form-label">密码</label> <div class=
"layui-input-inline"> <input type="password" name="password" lay-verify=
"required|pass"placeholder="请输入密码" autocomplete="off" class="layui-input">
</div> </div> <div class="layui-form-item"> <label class="layui-form-label">姓名
</label> <div class="layui-input-inline"> <input type="text" name="name"
lay-verify="required|" required placeholder="请输入用户名" autocomplete="off" class=
"layui-input"> </div> </div> <div class="layui-form-item"> <label class=
"layui-form-label">手机</label> <div class="layui-input-inline"> <input type=
"tel"name="phone" lay-verify="required|number" placeholder="请输入使用者手机号码"
lay-verType="tips" autocomplete="off" class="layui-input"> </div> </div> <div
class="layui-form-item"> <label class="layui-form-label">权限</label> <div
class="layui-input-inline"> <select name="power" lay-verify="required|"
lay-filter="interest-search" lay-search> <option value=""></option> <option
value="一般管理员">一般管理员</option> <option value="超级管理员" >超级管理员</option> </select>
</div> </div> <div class="layui-form-item"> <label class="layui-form-label">状态
</label> <div class="layui-input-inline"> <select name="status" lay-verify=
"required|"lay-filter="interest-search" lay-search> <option value=""></option>
<optionvalue="启用">启用</option> <option value="停用" >停用</option> </select>
</div> </div> <div class="layui-form-item"> <div class="layui-input-block">
<buttonclass="layui-btn" lay-submit lay-filter="*">添加</button> <button type=
"reset"class="layui-btn layui-btn-primary">重置</button> </div> </div> </form>  
form表单的验证js:


<script> layui.use('form', function(){ var form = layui.form; //自定义验证规则 form.
verify({ title: function(value){ if(value.length < 5){ return '账号不得小于5个字符'; } }
,pass: [/(.+){6,12}$/, '密码必须6到12位'] }); //事件监听 form.on('select', function(data){
console.log('select: ', this, data); }); form.on('select(quiz)', function(data){
console.log('select.quiz:', this, data); }); form.on('select(interest)',
function(data){ console.log('select.interest: ', this, data); }); form.on(
'checkbox', function(data){ console.log(this.checked, data.elem.checked); });
form.on('switch', function(data){ console.log(data); }); form.on('radio',
function(data){ console.log(data); }); //监听提交 form.on('submit(*)', function
(data){console.log(data); }); }); </script>
layui数据表格的全部js:


<script> layui.use('table', function(){ var table = layui.table; //渲染 table.
render({ elem: '#test' //绑定table表格 ,height: 450 ,url: '<%=
request.getContextPath()%>/admin/backAdmins' //后台springmvc接收路径 ,page:true
//true表示分页 ,limit: 10 ,id:'adminstable' ,toolbar: '#toolbarDemo' ,cols: [[ {type
:'checkbox', fixed: 'left'} ,{field:'account', title:'账号', width:120} ,{field:
'password', title:'密码', width:120} ,{field:'name', title:'姓名', width:100} ,{
field:'phone', title:'电话', width:120} ,{field:'power', title:'权限', width:100} ,{
field:'status', title:'状态', width:120} ,{field:'right', title: '操作', width:177,
toolbar:"#barDemo"} ]] }); //监听表格行点击 table.on('tr', function(obj){ console.log
(obj) }); //监听表格复选框选择 table.on('checkbox(test)', function(obj){ console.log
(obj) }); //监听表格单选框选择 table.on('radio(test2)', function(obj){ console.log(obj)
}); //监听单元格编辑 table.on('edit(test2)', function(obj){ var value = obj.value
//得到修改后的值 ,data = obj.data //得到所在行所有键值 ,field = obj.field; //得到字段 }); //监听工具条
table.on('tool(test)', function(obj){ var data = obj.data; if(obj.event ===
'del'){ layer.confirm('真的删除行么', function(index){ $.ajax({ url: "<%=
request.getContextPath()%>/admin/delAdmin", type: "POST", data:{"id":data.id},
dataType: "json", success: function(data){ obj.del(); layer.close(index); layer.
msg("删除成功", {icon: 6}); }, error:function (data) { layer.msg("删除失败", {icon: 5});
} }); }); } else if(obj.event === 'edit'){ //这里一般是发送修改的Ajax请求 EidtUv(data,obj);
} }); function EidtUv(data,obj) { $("#id").val(data.id); $("#account").val(data.
account); $("#password").val(data.password); $("#name").val(data.name); $(
"#phone").val(data.phone); $("#power").val(data.power); $("#status").find(
"option[value = '"+data.status+"']").attr("selected","selected"); layer.open({
title:'修改管理员', type:1, area:['400px','400px'], content:$("#form1") }); } var $ =
layui.jquery, active = { getCheckData: function(){//获取选中数据 var checkStatus =
table.checkStatus('adminstable') ,data = checkStatus.data; layer.alert(JSON.
stringify(data)); } ,getCheckLength: function(){//获取选中数目 var checkStatus =
table.checkStatus('adminstable') ,data = checkStatus.data; layer.msg('选中了:'+
data.length + ' 个'); } ,isAll: function(){//验证是否全选 var checkStatus = table.
checkStatus('adminstable'); layer.msg(checkStatus.isAll ? '全选': '未全选') } ,
parseTable: function(){ table.init('parse-table-demo', { limit: 3 }); } ,add:
function(){ layer.open({ title:'增加管理员', type:1, area:['400px','400px'], content
:$("#form") }); } ,delete: function(){ layer.confirm('确认删除吗?', function(index){
table.deleteRow('test') layer.close(index); }); } ,reload:function () { var
keyWord=$("#keyWord").val(); var keyType=$("#key_type option:selected").val();
table.reload('adminstable',{ method:'post', where:{keyWord:keyWord,keyType
:keyType} }); } }; $('i').on('click', function(){ var type = $(this).data('type'
); active[type] ? active[type].call(this) : ''; }); $('.layui-btn').on('click',
function(){ var type = $(this).data('type'); active[type] ? active[type].call(
this) : ''; }); $('#exportExcel').on('click',function () { alert(
"expppppppppppp"); var checkStatus = table.checkStatus('adminstable') ,data =
checkStatus.data; layer.alert(JSON.stringify(data)); $.ajax({ type:"post", url:
"<%=request.getContextPath()%>/export", async:true, contentType :
"application/json", dataType: "json", data:{ list:JSON.stringify(data) },
success: function( data ) { alert("sdfsf") } }); }) }); function exportExcel() {
alert("export") var checkStatus = table.checkStatus('contenttable') ,data =
checkStatus.data; alert("export:"+JSON.stringify(data)); $.ajax({ type:"post",
url: "<%=request.getContextPath()%>/export", async:false, dataType: "json", data
:{list:"data" }, success: function( data ) { alert("sdfsf") } }); } </script>
springmvc控制层:


/** * 增加管理员账号 * @param admins * @return */ @RequestMapping("/addAdmin")
publicString addAdmin(Admins admins){ System.out.println("增加用户开始:"); int index=
adminsService.addAdmin(admins); return "redirect:/admin/backAdminsjsp"; }
重定向回到页面:


/** * 跳转到后台管理员页面 * @return */ @RequestMapping("/backAdminsjsp") public String
backAdminsjsp(){ return "/backpages/adminslist"; }









我的座右铭:不会,我可以学;落后,我可以追赶;跌倒,我可以站起来;我一定行。

友情链接
ioDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信