1.html
<table class="layui-hide" id="test" lay-filter="test"></table>
2.初始化数据
layui.use(['table','form'], function(){
table = layui.table;
table.render({
elem: '#test'
,url:'接口地址'
,cellMinWidth: 80
,cols: [[
{field:'id', title: 'ID', sort: true,align: 'center'}
,{field:'name', title: '类别名称', sort: true,align: 'center'}
,{field:'level', title: '级别', minWidth: 150,align: 'center',}
,{fixed: 'right', width:250,align:'center', toolbar: '#barDemo'}
]]
});
3.根据添加接返回数据重新渲染table
$.ajax({
type:'POST', // 规定请求的类型(GET 或 POST)
url:'接口地址', // 请求的url地址
dataType:'json', //预期的服务器响应的数据类型
data:{
name: title,
sort:sort,
pid:pid,
isshow:isshow,
},//规定要发送到服务器的数据
beforeSend:function(){ //发送请求前运行的函数(发送之前就会进入这个函数)
},
success: function(result){ // 当请求成功时运行的函数
//...
if(result.code == 1){
layer.close(addLabelmodel);
$("#labeladdBox").hide();
table.reload('test', {
url: '/admin/workerorder/labelist/'
,where: {} //设定异步数据接口的额外参数
//,height: 300
,page: {
curr: 1 //重新从第 1 页开始
}
});
}else{
layer.msg(result.msg);
}
},
error:function(result){ //失败的函数
//...
},
complete:function(){ //请求完成时运行的函数(在请求成功或失败之后均调用,即在 success 和 error 函数之后,不管成功还是失败 都会进这个函数)
// ...
}
});