layui.use(['laytpl', 'table', 'jquery', 'form', 'miniTab', 'tree', 'xmSelect', 'laydate'], function () { let $ = layui.jquery, form = layui.form, table = layui.table, layer = layui.layer, tree = layui.tree, laydate = layui.laydate, // clipboard = layui.clipboard, xmSelect = layui.xmSelect, miniTab = layui.miniTab; /**** index begin ***/ //index页面 if ($('.location-index-page').length > 0) { miniTab.listen(); laydate.render({ elem: '#publish-date', }); // 渲染表格 let listUrl = $('#table-container').data('url'); let insTb = table.render({ elem: '#table-container', toolbar: '#toolbar-tpl', defaultToolbar: ['filter', 'exports', { //自定义头部工具栏右侧图标。如无需自定义,去除该参数即可 title: '搜索' , layEvent: 'search' , icon: 'layui-icon-search' }], url: listUrl, method: 'post', even: true, limits: [10,20,50,100,200,500,1000], request: { pageName: 'page', limitName: 'size', }, parseData: function (res) { return { "code": res.code, //解析接口状态 "msg": res.msg, //解析提示文本 "count": res.data.total, //解析数据长度 "data": res.data.list //解析数据列表 }; }, page: true, cols: [[ {type: 'checkbox'}, {field: 'id', minWidth: 80, title: '编号ID'}, {field: 'title', minWidth: 250, title: '标题'}, {field: 'category_title', minWidth: 100, title: '栏目'}, {field: 'nickname', minWidth: 100, title: '发布人'}, {field: 'published_at', minWidth: 200, title: '发布时间'}, // {field: 'views', minWidth: 80, title: '查看量'}, // {field: 'collects', minWidth: 80, title: '收藏量'}, // {field: 'likes', minWidth: 80, title: '点赞量'}, // {field: 'shares', minWidth: 80, title: '分享量'}, {field: 'sort', minWidth: 60, title: '排序', edit: 'text'}, // {templet: '#row-hot', field: 'hot', minWidth: 120,align: 'center', title: '热门'}, // {templet: '#row-check', field: 'is_check', minWidth: 120,align: 'center', title: '内容审核'}, {templet: '#row-operate', minWidth: 250, field: 'right', align: 'center', title: '操作', fixed: 'right'} ]], done: function () { Tools.setInsTb(insTb); } }); let modifyUrl = $('#row-modify').data('url'); // switch变更 function changeSwitch(filter) { form.on('switch(' + filter + ')', function (obj) { let val = obj.elem.checked ? 1 : 0; $.post(modifyUrl, {id: this.value, field: this.name, value: val}, function (res) { layer.msg(res.msg) if (res.code !== 0) { //操作不成功则刷新页面 insTb.reload(); } }) }); } //监听工具条 注意区别toolbar和tool toolbar是表头上的工具条 tool是行中的工具条 table.on('toolbar(table-container)', function (obj) { let layEvent = obj.event; let insTb = Tools.getInsTb(); let url = $($(this).context).data('href') let title = $($(this).context).data('title') let width = $($(this).context).data('width') ? $($(this).context).data('width') : '100%'; let height = $($(this).context).data('height') ? $($(this).context).data('height') : '100%'; let checkStatus = table.checkStatus('table-container'); let selected = checkStatus.data; let ids = []; // debugger; switch (layEvent) { case 'check': if (checkStatus.data.length <= 0) { layer.msg('请先选择数据'); return false; } $.each(selected, function (index, val) { ids.push(val.id); }) if (ids.length > 500) { layer.msg('一次操作请不要超过500条'); return false; } url += '?id=' + ids; openLayer(url, title, width, height); return false; // toolbar 删除 case 'del': if (checkStatus.data.length <= 0) { layer.msg('请先选择数据'); return false; } // let selected = checkStatus.data; // let ids = []; $.each(selected, function (index, val) { ids.push(val.id); }) delRow(url, ids, insTb); return false; // toolbar 刷新 case 'refresh': refreshTab(insTb); return false; // toolbar 搜索 case 'search': let search = $('.table-search-fieldset'); if (search.hasClass('div-show')) { search.css('display', 'none').removeClass('div-show'); } else { search.css('display', 'block').addClass('div-show'); } return false; // 其他 默认为打开弹出层 default: if (layEvent !== 'LAYTABLE_COLS' && layEvent !== 'LAYTABLE_EXPORT') { openLayer(url, title, width, height); return false; } } }); // 监听搜索操作 form.on('submit(data-search-btn)', function (data) { let categoryIds = []; let checkData = $('#ids-data').val(); categoryIds = checkData.split(','); data.field.category_id = categoryIds; //执行搜索重载 table.reload('table-container', { page: {curr: 1} , where: {searchParams: data.field} }, 'data'); return false; }); //监听行工具条 table.on('tool(table-container)', function (obj) { let data = obj.data; let layEvent = obj.event; let url = $($(this).context).data('href'); let title = $($(this).context).data('title'); let width = $($(this).context).data('width') ? $($(this).context).data('width') : '100%'; let height = $($(this).context).data('height') ? $($(this).context).data('height') : '100%'; let insTb = Tools.getInsTb(); switch (layEvent) { // 行 删除 case 'del': let ids = [data.id]; delRow(url, ids, insTb); return false; // 复制小程序路径 case 'path': let path = $($(this).context).data('path'); // layer.msg('路径复制成功 '+ path + '?id='+data.id+'&category_id='+data.category_id); new ClipboardJS('.clipboard-btn', { text: function() { return path + '?id='+data.id+'&category_id='+data.category_id; } }); layer.msg('路径复制成功!'); return false; //其他 默认为打开弹出层 default: openLayer(url, title, width, height); return false; } }); changeSwitch('changeRecommend');//监听推荐改变 changeSwitch('changeTop');//监听置顶 changeSwitch('changeHot');//监听热门 changeSwitch('changeCheck');//监听内容审核 } /*** index end ***/ // add和edit页面 if ($('.location-operate-page').length > 0) { let parentMenu = $('#parent-menu'); let menuList = parentMenu.data('menu') ? parentMenu.data('menu') : []; let showList = parentMenu.data('show') ? parentMenu.data('show') : [];//各栏目 可展示字段列表 xmSelect.render({ el: '#parent-menu', paging: false, autoRow: true, radio: true, clickClose: true, name: 'category_id', tips: '请选择栏目', direction: 'auto', height: 'auto', model: { icon: 'hidden', }, prop: { name: 'title', value: 'id', }, tree: { show: true, strict: false, clickCheck: true, expandedKeys: true, clickExpand: false }, theme: { color: '#1e84ff', }, data: menuList, on: function (data) { //arr: 当前多选已选中的数据 let arr = data.arr; //change, 此次选择变化的数据,数组 let change = data.change; //isAdd, 此次操作是新增还是删除 // let isAdd = data.isAdd; // console.log(arr, 'arr') // console.log(change, 'change') // console.log('已有: '+arr.length+' 变化: '+change.length+', 状态: ' + isAdd, 'desc') if (arr.length > 0 && change.length > 0) { //切换 $('.field-div').hide();//全部隐藏 let currentCateId = arr[0].id;//当前栏目ID let showFields = showList[currentCateId] ? showList[currentCateId] : []; // console.log(showFields, 'show current') // 显示选择栏目的字段 $.each(showFields, function (index, val) { $('.current-field-' + val).show(); }) } }, }); } });