layui.use([ 'laypage', 'layer', 'table', 'element','form','treetable','upload'], function(){
var laypage = layui.laypage //分页
,$ = layui.jquery
,layer = layui.layer //弹层
,table = layui.table //表格
,element = layui.element //元素操作
,form = layui.form
,upload = layui.upload
,treetable = layui.treetable;
if($('#menulist').attr('lay-filter') == 'menulist'){
var renderTable = function (wheres = {}) {
layer.load(2);
treetable.render({
treeColIndex:2,
treeSpid:0,
treeIdName: 'id',
treePidName: 'pid',
treeDefaultClose: true,
treeLinkage: false,
elem: '#menulist',
url: '/frontend/indexfrom',
page: false,
where:wheres,
id:'tablerReload',
cols: [[
{type:'numbers',title:'序号'}
,{field: 'id', title: 'ID', width:80, sort: true, align:'center'}
,{field: 'name', title: '菜单名称', align:'center'}
,{field: 'url', title: '地址', width:300,align:'center'}
,{field: 'icon', title: '图标', width:120,align:'center',templet:function(d){
return ''
}}
,{field: 'sort', title: '排序',width:80,edit: 'text',align:'center',sort: true,templet:"#sortBox"}
,{field: 'status', title: '状态',width:70, align:'center', templet:"#Toexamine",templet:function(d){
if(d.status === '2'){
return ''
} else {
return ''
}
}}
,{fixed: 'right',title:'操作', width: 200, align:'center', toolbar: '#operate'}
]],
done: function () {
layer.closeAll('loading');
}
});
};
renderTable();
}
// table.reload('tablerReload'); //重新加载
//列表操作
table.on('tool(menulist)', function(obj){
var layEvent = obj.event,
data = obj.data;
switch (layEvent){
case 'edit':
layer.open({
type: 2,
title: '编辑',
closeBtn: 1, //不显示关闭按钮
shade: [0],
area: ['600px', '500px'],
anim: 2,
content: '/frontend/edit?id='+data.id, //iframe的url,no代表不显示滚动条
end:function () {
renderTable();
}
});
break;
case 'del':
layer.confirm('您确认要删除吗?',{btn:['确认','取消']},function () {
$.post('/frontend/frontendmenusortedit',{'id':data.id,del:data.del,type:'del'},function (data) {
if(data.code == 200)
{
layer.msg(data.msg);
// table.reload('menulist');
} else if(data.code == 300 ) {
layer.msg(data.msg);
}
})
})
break;
case 'addson':
layer.open({
type: 2,
title: '添加子菜单',
closeBtn: 1, //不显示关闭按钮
shade: [0],
area: ['600px', '100%'],
anim: 2,
content: '/frontend/create?id='+data.id+'&name='+data.name, //iframe的url,no代表不显示滚动条
end:function () {
renderTable();
}
});
break;
case 'produce':
layer.open({
type: 2,
title: '生成['+data.name+']控制模板代码',
closeBtn: 1, //不显示关闭按钮
shade: [0],
area: ['600px', '500px'],
anim: 2,
content: '/frontend/produce?id='+data.id+'&name='+data.name, //iframe的url,no代表不显示滚动条
end:function () {
renderTable();
}
});
break;
}
});
var $ = layui.$, active = {
addData: function(){ //获取选中数据
layer.open({
type: 2,
title: '添加导航菜单',
closeBtn: 1, //不显示关闭按钮
shade: [0],
area: ['600px', '70%'],
anim: 2,
content: '/frontend/parentadd', //iframe的url,no代表不显示滚动条
end:function () {
renderTable();
}
});
}
};
$('.demoTable .layui-btn').on('click', function(){
var type = $(this).data('type');
active[type] ? active[type].call(this) : '';
});
form.on('submit(editMenu)', function(data){
var index = layer.msg('数据提交中,请稍候',{icon: 16,time:false,shade:0.8}); //数据提交提示
var formData = new FormData($('form')[0]);
$.ajax({
cache : false,
type : "post",
url : "/frontend/editfrom",
data : formData, // 你的formid
contentType: false, //jax 中 contentType 设置为 false 是为了避免 JQuery 对其操作,从而失去分界符,而使服务器不能正常解析文件
processData: false, //当设置为true的时候,jquery ajax 提交的时候不会序列化 data,而是直接使用data
error : function(request) {
layer.close(index);
layer.msg("请联系管理员");
},
success : function(data) {
if(data.data != null){
layer.msg(ReturnInfo(data.data),{icon:5});
return ;
}
layer.msg(data.msg);
parent.layer.close(oindex); //再执行关闭
//layer.close(index);
}
});
return false;
});
//添加页面提交数据
form.on('submit(addmenu)', function(data){
layer.msg('数据提交中,请稍候',{icon: 16,time:false,shade:0.8}); //数据提交提示
var formData = new FormData($('form')[0]);
/*if(formData.getAll('img')[0].size == 0){
layer.msg('请选择图片');
return false;
}*/
$.ajax({
type : "post",
url : "/frontend/createfrom",
data : formData,
contentType: false, //jax 中 contentType 设置为 false 是为了避免 JQuery 对其操作,从而失去分界符,而使服务器不能正常解析文件
processData: false, //当设置为true的时候,jquery ajax 提交的时候不会序列化 data,而是直接使用data
error : function(request) {
layer.msg("请联系管理员");
},
success : function(data) {
if(data.data != null){
layer.msg(ReturnInfo(data.data),{icon:5});
return ;
}
layer.msg(data.msg);
parent.layer.close(oindex); //再执行关闭
}
});
return false;
});
//监听指定开关
form.on('switch(switchTest)', function(data){
layer.confirm('确定要修改此操作吗?',{btn: ['确定', '取消']},function () {
$.post('/frontend/frontendmenusortedit',{id:data.value,examineid:1,type:'status'},function (data) {
if(data.code == 200)
{
layer.msg(data.msg);
//table.reload('tablerReload');
parent.layui.table.reload('menulist');
} else if(data.code == 300 ) {
layer.msg(data.msg);
}
})
},function (aa) {
// table.reload('tablerReload');
parent.layui.table.reload('menulist'); //重新加载
})
});
//添加图片上传
upload.render({
elem: '#test1'
,url: '/house/createimg'
,data:{token:csrfToken}
,field:'img'
,size:1000 //限制上传图片的大小,单位为KB
,exts: 'png|jpg|jpeg' //只允许上传压缩文件
,auto:false
,bindAction:'#addcity'
,choose: function(obj){
obj.preview(function(index, file, result){
$('#breviary_img').attr('src', result); //图片链接(base64)
});
}
});
/* 列表操作 edit 修改后执行 */
table.on('edit(menulist)', function(obj){
var data = obj.data;
if(obj.field="sort"){
$.post('/frontend/frontendmenusortedit',{id:data.id,sort:data.sort,type:'sort'},function (data) {
if(data.code == 200)
{
layer.msg(data.msg);
} else if(data.code == 300 ) {
layer.msg(data.msg);
}
})
}
})
});