1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| <script src="${pageContext.request.contextPath}/resources/layui/layui.all.js"></script> <script> //JavaScript代码区域 layui.use('element', function(){ var element = layui.element; });
//显示或隐藏文章 layui.use('form',function(){ var form = layui.form form.on('switch(display)', function(obj){ //根据业务判断是开启还是关闭 var state = obj.elem.checked?0:1; //取数据(根据索引table.cache里面的行数据) var index = obj.othis.parents('tr').attr("data-index"); var id = tableData[index].id; $.ajax({ url: '../PostController/display?id='+id+'&display='+state, type: "post", dataType:"json", success: function(suc) { if(suc.code === 1) { if(suc.msg === 0){ layer.msg("文章已隐藏", { icon: 6 }); }else if(suc.msg === 1){ layer.msg("文章已显示", { icon: 6 }); } } else { layer.msg("设置失败,请稍后再试!", { icon: 5 }); } } }); }); });
var tableData;
layui.use('table', function(){ var table = layui.table;
table.render({ elem: '#test' ,url:'../PostController/findByPage' ,method:'post' ,limits : [5,10,15,20] ,limit : 10 ,request: { pageName: 'curr' , limitName: 'nums' } ,toolbar: '#toolbarDemo' ,defaultToolbar: ['filter', 'exports', 'print', { title: '提示' ,layEvent: 'LAYTABLE_TIPS' ,icon: 'layui-icon-tips' }] ,title: '用户数据表' ,cols: [[ {type: 'checkbox', fixed: 'left'} ,{field:'id', title:'ID', width:70, unresize: true, sort: true} ,{field:'title', title:'标题',width:328} ,{field:'typeString', title:'分类',width:158} ,{field:'clickhit', title:'点击数',width:125, sort: true} ,{field:'replyhit', title:'评论数',width:125, sort: true} ,{field:'timeString', title:'发表时间',width:188, sort: true} ,{field:'display', title:'显示状态',width:120,templet:"#switchTpl"}
,{fixed: '', title:'操作', fixed: 'right', width:132, toolbar: '#barDemo'} ]] ,page: true ,id:"tableIns" ,done:function(){ tableData = table.cache.tableIns; } }); table.on('toolbar(test)', function(obj){ var checkStatus = table.checkStatus(obj.config.id); switch(obj.event){ case 'getCheckData': var data = checkStatus.data; layer.alert(JSON.stringify(data)); break; case 'getCheckLength': var data = checkStatus.data; layer.msg('选中了:'+ data.length + ' 个'); break; case 'isAll': layer.msg(checkStatus.isAll ? '全选': '未全选'); break; case 'LAYTABLE_TIPS': layer.alert('这是工具栏右侧自定义的一个图标按钮'); break; }; }); table.on('tool(test)', function(obj){ var data = obj.data ,layEvent = obj.event; console.log(obj) switch(layEvent){ case 'del': var delIndex = layer.confirm('真的删除"' + data.title + '"吗?', function(delIndex) { $.ajax({ url: '../PostController/delete?id='+data.id, type: "post", dataType:"json", success: function(suc) { if(suc.code === 1) { obj.del(); layer.close(delIndex); console.log(delIndex); layer.msg("删除成功", { icon: 1 }); } else { layer.msg("删除失败", { icon: 5 }); } } }); layer.close(delIndex); }); break; case 'edit':
window.location.href="../PostController/editPost?id="+data.id; break; } }); }); </script>
|