layui_list.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <!-- <title>layui规则管理</title>-->
  6. <title>规则管控</title>
  7. <meta name="description" content="不会写前端只好用layui">
  8. <meta name="author" content="道长"/>
  9. <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
  10. <link rel="icon" href="/static/img/logo.png" type="image/x-icon">
  11. <link rel="stylesheet" href="/static/plugin/layui/css/layui.css">
  12. <script src="/static/js/jquery.min.js"></script>
  13. <script src="/static/js/grey.js"></script>
  14. <script src="/static/plugin/layui/layui.js"></script>
  15. </head>
  16. <body>
  17. <table class="layui-hide" id="test" lay-filter="test"></table>
  18. <script type="text/html" id="toolbarDemo">
  19. <div class="layui-btn-container">
  20. <button class="layui-btn layui-btn-sm layui-bg-blue" id="moreTest">
  21. 多选操作
  22. <i class="layui-icon layui-icon-down layui-font-12"></i>
  23. </button>
  24. <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
  25. <button class="layui-btn layui-btn-sm" lay-event="getData">获取当前页数据</button>
  26. <button class="layui-btn layui-btn-sm" lay-event="isAll">是否全选</button>
  27. <button class="layui-btn layui-btn-sm" id="reloadTest">
  28. 重载测试
  29. <i class="layui-icon layui-icon-down layui-font-12"></i>
  30. </button>
  31. <button class="layui-btn layui-btn-sm layui-btn-primary" lay-event="multi-row">
  32. 多行
  33. </button>
  34. <button class="layui-btn layui-btn-sm layui-btn-primary" lay-event="default-row">
  35. 单行
  36. </button>
  37. <button class="layui-btn layui-btn-sm layui-btn-primary" lay-event="jxs">
  38. 解析管理
  39. </button>
  40. </div>
  41. </script>
  42. <script type="text/html" id="cityTpl">
  43. <select id="demoCity1" class="layui-border" lay-ignore>
  44. <option value="浙江杭州">浙江杭州</option>
  45. <option value="江西南昌">江西南昌</option>
  46. <option value="湖北武汉">湖北武汉</option>
  47. </select>
  48. </script>
  49. <script type="text/html" id="barDemo">
  50. <!-- <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>-->
  51. <a class="layui-btn layui-btn-xs" lay-event="set_top">顶</a>
  52. <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="set_bottom">底</a>
  53. <a class="layui-btn layui-btn-xs layui-btn-info" lay-event="set_order">输</a>
  54. <!-- <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">隐藏</a>-->
  55. </script>
  56. <script>
  57. layui.use(['table', 'dropdown'], function(){
  58. var table = layui.table;
  59. var dropdown = layui.dropdown;
  60. var $ = layui.$;
  61. var form = layui.form;
  62. layer.msg('本页面展示数据为未来功能,<br>可能并没有实际作用,等道长弃坑后续有缘人补上。', {
  63. closeBtn: 1,
  64. icon: 6,
  65. time: 21*1000,
  66. offset: '21px'
  67. });
  68. function setState(data,to_set_state){
  69. if(data.length < 1){
  70. return layer.msg('请至少选择一行');
  71. }
  72. to_set_state = to_set_state||0;
  73. console.log(data);
  74. let site_names = data.map(it=>it.site_name);
  75. let site_names_str = site_names.join(',');
  76. console.log('准备将'+site_names_str+'的显示状态设置为:'+to_set_state);
  77. let params = {"names":site_names_str};
  78. console.log(params);
  79. $.post("/admin/rule_state/"+to_set_state,params,function(data,status){
  80. console.log(data);
  81. if(data.code === 200){
  82. // alert(data.msg);
  83. // location.reload();
  84. table.reload('test', {
  85. // page: {
  86. // curr: 3 //重新从第 1 页开始
  87. // }
  88. });
  89. }else{
  90. console.log('修改失败了...');
  91. return false
  92. }
  93. });
  94. }
  95. function setOrder(data,to_set_order){
  96. if(data.length < 1){
  97. return layer.msg('请至少选择一行');
  98. }
  99. to_set_order = to_set_order||0;
  100. console.log(data);
  101. let site_names = data.map(it=>it.site_name);
  102. let site_names_str = site_names.join(',');
  103. console.log('准备将'+site_names_str+'的显示状态设置为:'+to_set_order);
  104. let params = {"names":site_names_str};
  105. console.log(params);
  106. $.post("/admin/rule_order/"+to_set_order,params,function(data,status){
  107. console.log(data);
  108. if(data.code === 200){
  109. // alert(data.msg);
  110. // location.reload();
  111. table.reload('test', {
  112. // page: {
  113. // curr: 3 //重新从第 1 页开始
  114. // }
  115. });
  116. layer.msg('已设置顺序为'+to_set_order,{time:1000});
  117. }else{
  118. console.log('修改失败了...');
  119. return false
  120. }
  121. });
  122. }
  123. // 创建渲染实例
  124. table.render({
  125. elem: '#test'
  126. ,url: '/layui/api/list' // 此处为静态模拟数据,实际使用时需换成真实接口
  127. ,toolbar: '#toolbarDemo'
  128. ,defaultToolbar: ['filter', 'exports', 'print', {
  129. title: '帮助'
  130. ,layEvent: 'LAYTABLE_TIPS'
  131. ,icon: 'layui-icon-tips'
  132. }]
  133. // ,height: 'full-200' // 最大高度减去其他容器已占有的高度差
  134. // ,cellMinWidth: 80
  135. // ,totalRow: true // 开启合计行
  136. ,page: true
  137. ,limit:12
  138. ,limits:[10,12,15,20,40,60,80,100,150,200,300,500,1000]
  139. ,cols: [[
  140. {type: 'checkbox', fixed: 'left'}
  141. // ,{field: 'id', fixed: 'left',title: 'ID', width: 20, sort: true,totalRowText: '合计:'}
  142. ,{field: 'id',title: 'ID', width: 60, sort: true}
  143. ,{field: 'site_name', title: '存储名称', width: 120}
  144. , {
  145. field: 'state', title: '显示状态', minWidth: 100, templet: function (res) {
  146. let menuId = res.site_name;
  147. if (res.state === 1) {
  148. return " <input type='checkbox' menuId = '" + menuId + "' lay-filter='state' lay-skin='switch' lay-text='显示|已隐藏' checked>"
  149. } else if (res.state === 0) {
  150. return " <input type='checkbox' menuId = '" + menuId + "' lay-filter='state' lay-skin='switch' lay-text='显示|已隐藏'>"
  151. }
  152. }
  153. }
  154. ,{field: 'order', title: '顺序', width: 80}
  155. ,{field: 'name', title: '显示名称', width: 120}
  156. ,{field: 'key', title: '唯一标识', width: 120}
  157. ,{field: 'type', title: '<i class="layui-icon layui-icon-email">类型</i>', minWidth: 80}
  158. ,{field: 'searchable', title: '可搜索', minWidth: 80}
  159. ,{field: 'quickSearch', title: '可快搜', minWidth: 80}
  160. ,{field: 'filterable', title: '可筛选', minWidth: 80}
  161. // ,{field:'city', width:115, title: '城市', minWidth:115, templet: '#cityTpl', exportTemplet: function(d, obj){
  162. // //console.log(obj)
  163. // // 处理该字段的导出数据
  164. // var td = obj.td(this.field); //获取当前 td
  165. // return td.find('select').val();
  166. // }}
  167. ,{fixed: 'right', title:'操作', width: 135, minWidth: 135, toolbar: '#barDemo'}
  168. ]]
  169. ,done: function(){
  170. var id = this.id;
  171. // 重载测试
  172. dropdown.render({
  173. elem: '#reloadTest' //可绑定在任意元素中,此处以上述按钮为例
  174. ,data: [{
  175. id: 'reload',
  176. title: '重载'
  177. },{
  178. id: 'reload-deep',
  179. title: '重载 - 参数叠加'
  180. },{
  181. id: 'reloadData',
  182. title: '仅重载数据'
  183. },{
  184. id: 'reloadData-deep',
  185. title: '仅重载数据 - 参数叠加'
  186. }]
  187. // 菜单被点击的事件
  188. ,click: function(obj){
  189. switch(obj.id){
  190. case 'reload':
  191. // 重载 - 默认(参数重置)
  192. table.reload('test', {
  193. where: {
  194. abc: '123456'
  195. //,test: '新的 test2'
  196. //,token: '新的 token2'
  197. }
  198. });
  199. break;
  200. case 'reload-deep':
  201. // 重载 - 深度(参数叠加)
  202. table.reload('test', {
  203. where: {
  204. abc: 123
  205. ,test: '新的 test1'
  206. }
  207. //,defaultToolbar: ['print'] // 重载头部工具栏右侧图标
  208. //,cols: ins1.config.cols
  209. }, true);
  210. break;
  211. case 'reloadData':
  212. // 数据重载 - 参数重置
  213. table.reloadData('test', {
  214. where: {
  215. abc: '123456'
  216. //,test: '新的 test2'
  217. //,token: '新的 token2'
  218. }
  219. ,scrollPos: 'fixed' // 保持滚动条位置不变 - v2.7.3 新增
  220. ,height: 2000 // 测试无效参数(即与数据无关的参数设置无效,此处以 height 设置无效为例)
  221. //,url: '404'
  222. //,page: {curr: 1, limit: 30} // 重新指向分页
  223. });
  224. break;
  225. case 'reloadData-deep':
  226. // 数据重载 - 参数叠加
  227. table.reloadData('test', {
  228. where: {
  229. abc: 123
  230. ,test: '新的 test1'
  231. }
  232. }, true);
  233. break;
  234. }
  235. layer.msg('可观察 Network 请求参数的变化');
  236. }
  237. });
  238. // 更多测试
  239. dropdown.render({
  240. elem: '#moreTest' //可绑定在任意元素中,此处以上述按钮为例
  241. ,data: [{
  242. id: 'show',
  243. title: '🟢显示'
  244. },{
  245. id: 'hide',
  246. title: '🔴隐藏'
  247. },
  248. {
  249. id:'set_top',
  250. title: '⬆️置顶',
  251. },
  252. {
  253. id:'set_order_number',
  254. title: '↕️指定序号',
  255. },
  256. {
  257. id:'set_bottom',
  258. title: '⬇️置底',
  259. },
  260. // {
  261. // id: 'add',
  262. // title: '添加'
  263. // },{
  264. // id: 'update',
  265. // title: '编辑'
  266. // },{
  267. // id: 'delete',
  268. // title: '删除'
  269. // }
  270. ]
  271. //菜单被点击的事件
  272. ,click: function(obj){
  273. var checkStatus = table.checkStatus(id)
  274. var data = checkStatus.data; // 获取选中的数据
  275. switch(obj.id){
  276. case 'show':
  277. // console.log(obj);
  278. setState(data,1)
  279. break;
  280. case 'hide':
  281. // console.log(obj);
  282. setState(data,0);
  283. break;
  284. case 'set_top':
  285. // console.log(obj);
  286. setOrder(data,0);
  287. break;
  288. case 'set_order_number':
  289. layer.prompt({
  290. formType: 0,
  291. value: '',
  292. title: '请输入指定的排序序号,数字越小排名越靠前',
  293. btn: ['确定','取消'], //按钮,
  294. btnAlign: 'c'
  295. }, function(value,index){
  296. console.log('value:'+value);
  297. var reg = /^[\d]+$/;
  298. if(!reg.test(value)){
  299. layer.msg('请输入有效数字!',{time:500});
  300. }else{
  301. setOrder(data,Number(value));
  302. layer.close(index);
  303. }
  304. });
  305. // setOrder(data,0);
  306. break;
  307. case 'set_bottom':
  308. // console.log(obj);
  309. setOrder(data,9999)
  310. break;
  311. case 'add':
  312. layer.open({
  313. title: '添加',
  314. type: 1,
  315. area: ['80%','80%'],
  316. content: '<div style="padding: 16px;">自定义表单元素</div>'
  317. });
  318. break;
  319. case 'update':
  320. if(data.length !== 1) return layer.msg('请选择一行');
  321. layer.open({
  322. title: '编辑',
  323. type: 1,
  324. area: ['80%','80%'],
  325. content: '<div style="padding: 16px;">自定义表单元素</div>'
  326. });
  327. break;
  328. case 'delete':
  329. if(data.length === 0){
  330. return layer.msg('请选择一行');
  331. }
  332. layer.msg('delete event');
  333. break;
  334. }
  335. }
  336. });
  337. }
  338. ,error: function(res, msg){
  339. console.log(res, msg)
  340. }
  341. });
  342. // 工具栏事件
  343. table.on('toolbar(test)', function(obj){
  344. var id = obj.config.id;
  345. var checkStatus = table.checkStatus(id);
  346. var othis = lay(this);
  347. switch(obj.event){
  348. case 'getCheckData':
  349. var data = checkStatus.data;
  350. layer.alert(layui.util.escape(JSON.stringify(data)));
  351. break;
  352. case 'getData':
  353. var getData = table.getData(id);
  354. console.log(getData);
  355. layer.alert(layui.util.escape(JSON.stringify(getData)));
  356. break;
  357. case 'isAll':
  358. layer.msg(checkStatus.isAll ? '全选': '未全选')
  359. break;
  360. case 'multi-row':
  361. table.reload('test', {
  362. // 设置行样式,此处以设置多行高度为例。若为单行,则没必要设置改参数 - 注:v2.7.0 新增
  363. lineStyle: 'height: 95px;'
  364. });
  365. layer.msg('即通过设置 lineStyle 参数可开启多行');
  366. break;
  367. case 'default-row':
  368. table.reload('test', {
  369. lineStyle: null // 恢复单行
  370. });
  371. layer.msg('已设为单行');
  372. break;
  373. case 'jxs':
  374. location.href = 'jxs';
  375. break;
  376. case 'LAYTABLE_TIPS':
  377. layer.alert('Table for layui-v'+ layui.v);
  378. break;
  379. };
  380. });
  381. //触发单元格工具事件
  382. table.on('tool(test)', function(obj){ // 双击 toolDouble
  383. var data = obj.data;
  384. //console.log(obj)
  385. function setPos(od,msg){
  386. let obj_name;
  387. try{
  388. obj_name = obj.data.api.match(/rule=(.*)/)[1].split('&')[0];
  389. }catch (e){
  390. obj_name = obj.data.key;
  391. }
  392. console.log(obj);
  393. let params = {"names":[obj_name].join(',')};
  394. console.log(params);
  395. $.post("/admin/rule_order/"+od,params,function(res,status){
  396. console.log(res);
  397. if(res.code === 200){
  398. data.order = od;
  399. // console.log(obj);
  400. obj.update({
  401. order: data.order
  402. });
  403. layer.msg('已'+msg);
  404. }else{
  405. console.log('修改失败了...');
  406. layer.msg(msg+'失败,具体错误看日志');
  407. return false
  408. }
  409. });
  410. }
  411. if(obj.event === 'del'){
  412. let obj_name;
  413. try{
  414. obj_name = obj.data.api.match(/rule=(.*)/)[1].split('&')[0];
  415. }catch (e){
  416. obj_name = obj.data.key;
  417. }
  418. layer.confirm('真的要隐藏规则'+obj_name+'么', function(index){
  419. console.log(obj);
  420. let params = {"names":[obj_name].join(',')};
  421. console.log(params);
  422. $.post("/admin/rule_state/0",params,function(res,status){
  423. console.log(res);
  424. if(res.code === 200){
  425. data.state = 0;
  426. obj.update({
  427. // 这里的字段必须要在 table.render.cols.filed 有定义,否则无法触发表格渲染
  428. // key 决定是否重新渲染某一列,value 由 templet 里的语句进行逻辑处理
  429. state: data.state
  430. });
  431. // $('input[menuid="'+obj_name+'"]').removeAttr('checked');
  432. // table.render();
  433. // alert(data.msg);
  434. // location.reload();
  435. }else{
  436. // alert(data.msg);
  437. console.log('修改失败了...');
  438. return false
  439. }
  440. });
  441. // obj.del(); //这是删除,隐藏规则是不需要删除规则的,最多也就刷新一下
  442. layer.close(index);
  443. });
  444. } else if(obj.event === 'edit'){
  445. layer.open({
  446. title: '编辑',
  447. type: 1,
  448. area: ['80%','80%'],
  449. content: '<div style="padding: 16px;">自定义表单元素</div>'
  450. });
  451. }else if(obj.event === 'set_top'){
  452. setPos(0,'置顶');
  453. }else if(obj.event === 'set_bottom'){
  454. setPos(9999,'置底');
  455. }else if(obj.event === 'set_order'){
  456. layer.prompt({
  457. formType: 0,
  458. value: '',
  459. title: '请输入指定的排序序号,数字越小排名越靠前',
  460. btn: ['确定','取消'], //按钮,
  461. btnAlign: 'c'
  462. }, function(value,index){
  463. console.log('value:'+value);
  464. var reg = /^[\d]+$/;
  465. if(!reg.test(value)){
  466. layer.msg('请输入有效数字!',{time:500});
  467. }else{
  468. setPos(Number(value),'指定顺序');
  469. layer.close(index);
  470. }
  471. });
  472. }
  473. });
  474. //触发表格复选框选择
  475. table.on('checkbox(test)', function(obj){
  476. console.log(obj)
  477. });
  478. //触发表格单选框选择
  479. table.on('radio(test)', function(obj){
  480. console.log(obj)
  481. });
  482. // 行单击事件
  483. table.on('row(test)', function(obj){
  484. //console.log(obj);
  485. //layer.closeAll('tips');
  486. });
  487. // 行双击事件
  488. table.on('rowDouble(test)', function(obj){
  489. console.log(obj);
  490. });
  491. // 单元格编辑事件
  492. table.on('edit(test)', function(obj){
  493. var field = obj.field //得到字段
  494. ,value = obj.value //得到修改后的值
  495. ,data = obj.data; //得到所在行所有键值
  496. var update = {};
  497. update[field] = value;
  498. obj.update(update);
  499. });
  500. form.on('switch(state)', function (data) {
  501. let site_name = data.elem.attributes['menuId'].nodeValue;
  502. let to_set_state = data.elem.checked?1:0;
  503. console.log('准备将'+site_name+'的显示状态设置为:'+to_set_state);
  504. let params = {"names":[site_name].join(',')};
  505. console.log(params);
  506. $.post("/admin/rule_state/"+to_set_state,params,function(data,status){
  507. console.log(data);
  508. if(data.code === 200){
  509. // alert(data.msg);
  510. // location.reload();
  511. }else{
  512. // alert(data.msg);
  513. console.log('修改失败了...');
  514. data.elem.checked = !to_set_state;
  515. return false
  516. }
  517. });
  518. // console.log(data.value); // 开关value值,也可以通过data.elem.value得到
  519. // let menuId = data.elem.attributes['menuId'].nodeValue;
  520. //console.log(data.elem); // 得到checkbox原始DOM对象
  521. // console.log(data.othis); // 得到美化后的DOM对象
  522. });
  523. });
  524. </script>
  525. </body>
  526. </html>