1、记录下layui,数据重载功能,由于写后台比较多,前端一些小问题困扰了好久。
<body>
<div class="demoTable">
<form class="layui-form layui-col-md12 x-so">
请输入用户ID:
<div class="layui-inline">
<input class="layui-input" name="id" id="demoReload" autocomplete="off">
</div>
<button class="layui-btn" data-type="reload" lay-submit lay-filter="formsearch">搜索</button>
</form>
</div>
<table class="layui-hide" id="LAY_table_user" lay-filter="user"></table>
<script type="text/html" id="toolbarDemo">
<div class="layui-btn-container">
<button class="layui-btn layui-btn-sm" lay-event="getCheckData">选择用户</button>
</div>
</script>
<script src="/auth/assets/lib/layui/layui.js" charset="utf-8"></script>
<script>
layui.use(['table', 'form', 'layer'], function(){
var table = layui.table , form = layui.form;
//方法级渲染
table.render({
elem: '#LAY_table_user'
,url: '@XXXXXXXX'
,toolbar: '#toolbarDemo'
// , defaultToolbar:['filter','exports','print'],
,cols: [[
{type:'numbers', width:80,},
{type:'checkbox', width:80,},
{field:'id', title: 'ID', width:80, sort: true, fixed: true},
{field: 'username', title: '用户名',},
]]
,id: 'testReload',
page:true,
limit: 10,
page: {
layout: ['prev', 'page', 'next', 'count'],
first: false,
last: false
},
});
form.on('submit(formsearch)', function(data){
var field = data.field;
//执行重载
table.reload('LAY_table_user', {
where: field
});
return false;
});
//搜索重载,我之前没注意要和后台数据对应,js还是不太熟
var $ = layui.$, active = {
reload: function(){
var demoReload = $('#demoReload');
layer.msg(demoReload.val());
//执行重载
table.reload('testReload', {
url: XXXXXX
,where: {
id: demoReload.val()
}
});
}
};
$('.demoTable .layui-btn').on('click', function(){
var type = $(this).data('type');
active[type] ? active[type].call(this) : '';
});
//头工具栏事件 传值给父窗体
table.on('toolbar(user)', function(obj){
var checkStatus = table.checkStatus(obj.config.id);
var data1 = obj.data;
switch(obj.event){
case 'getCheckData':
var data = checkStatus.data;
var data2 = JSON.stringify(data);
var obj1 = JSON.parse(data2);
// json字符串转换成json对象
layer.alert(obj1[0].id);
parent.$('#userid').val(obj1[0].id);
var index = parent.layer.getFrameIndex(window.name);
parent.layer.close(index);
break;
};
});
});
</script>
</body>
2、传值给父窗体的时候,传过去的值总是闪一下就被刷新了,原因是自己用了x_admin_show这个函数,
end:function(){
location.reload();
}
这个语句表示的就是刷新界面,把上面那句注释掉,重写x_admin_show函数。即可(还是代码不熟挖的坑,23333333333)
function x_admin_show(title,url,w,h){
if (title == null || title == '') {
title=false;
};
if (url == null || url == '') {
url="404.html";
};
if (w == null || w == '') {
w=($(window).width()*0.9);
};
if (h == null || h == '') {
h=($(window).height() - 50);
};
layer.open({
type: 2,
area: [w+'px', h +'px'],
fix: false, //不固定
maxmin: true,
shadeClose: true,
shade:0.4,
title: title,
content: url,
end:function(){
location.reload();
}
});
}