menu.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. define(["jquery", "easy-admin", "treetable", "iconPickerFa", "autocomplete"], function ($, ea) {
  2. var table = layui.table,
  3. treetable = layui.treetable,
  4. iconPickerFa = layui.iconPickerFa,
  5. autocomplete = layui.autocomplete;
  6. var init = {
  7. table_elem: '#currentTable',
  8. table_render_id: 'currentTableRenderId',
  9. index_url: 'system.menu/index',
  10. add_url: 'system.menu/add',
  11. delete_url: 'system.menu/delete',
  12. edit_url: 'system.menu/edit',
  13. modify_url: 'system.menu/modify',
  14. };
  15. return {
  16. index: function () {
  17. var renderTable = function () {
  18. layer.load(2);
  19. treetable.render({
  20. where: {limit: 9999},
  21. treeColIndex: 1,
  22. treeSpid: 0,
  23. homdPid: 99999999,
  24. treeIdName: 'id',
  25. treePidName: 'pid',
  26. url: ea.url(init.index_url),
  27. elem: init.table_elem,
  28. id: init.table_render_id,
  29. toolbar: '#toolbar',
  30. page: false,
  31. skin: 'line',
  32. // @todo 不直接使用ea.table.render(); 进行表格初始化, 需要使用 ea.table.formatCols(); 方法格式化`cols`列数据
  33. cols: ea.table.formatCols([[
  34. {type: 'checkbox'},
  35. {field: 'title', width: 250, title: '菜单名称', align: 'left'},
  36. {field: 'icon', width: 80, title: '图标', templet: ea.table.icon},
  37. {field: 'href', minWidth: 120, title: '菜单链接'},
  38. {
  39. field: 'is_home',
  40. width: 80,
  41. title: '类型',
  42. templet: function (d) {
  43. if (d.pid === 99999999) {
  44. return '<span class="layui-badge layui-bg-blue">首页</span>';
  45. }
  46. if (d.pid === 0) {
  47. return '<span class="layui-badge layui-bg-gray">模块</span>';
  48. } else {
  49. return '<span class="layui-badge-rim">菜单</span>';
  50. }
  51. }
  52. },
  53. {field: 'status', title: '状态', width: 85, templet: ea.table.switch},
  54. {field: 'sort', width: 80, title: '排序', edit: 'text'},
  55. {
  56. width: 230,
  57. title: '操作',
  58. templet: ea.table.tool,
  59. operat: [
  60. [{
  61. text: '添加下级',
  62. url: init.add_url,
  63. method: 'open',
  64. auth: 'add',
  65. class: 'layui-btn layui-btn-xs layui-btn-normal',
  66. }, {
  67. text: '编辑',
  68. url: init.edit_url,
  69. method: 'open',
  70. auth: 'edit',
  71. class: 'layui-btn layui-btn-xs layui-btn-success',
  72. }],
  73. 'delete'
  74. ]
  75. }
  76. ]], init),
  77. done: function () {
  78. layer.closeAll('loading');
  79. }
  80. });
  81. };
  82. renderTable();
  83. $('body').on('click', '[data-treetable-refresh]', function () {
  84. renderTable();
  85. });
  86. $('body').on('click', '[data-treetable-delete]', function () {
  87. var tableId = $(this).attr('data-treetable-delete'),
  88. url = $(this).attr('data-url');
  89. tableId = tableId || init.table_render_id;
  90. url = url != undefined ? ea.url(url) : window.location.href;
  91. var checkStatus = table.checkStatus(tableId),
  92. data = checkStatus.data;
  93. if (data.length <= 0) {
  94. ea.msg.error('请勾选需要删除的数据');
  95. return false;
  96. }
  97. var ids = [];
  98. $.each(data, function (i, v) {
  99. ids.push(v.id);
  100. });
  101. ea.msg.confirm('确定删除?', function () {
  102. ea.request.post({
  103. url: url,
  104. data: {
  105. id: ids
  106. },
  107. }, function (res) {
  108. ea.msg.success(res.msg, function () {
  109. renderTable();
  110. });
  111. });
  112. });
  113. return false;
  114. });
  115. $('body').on('click', '[data-treetable-arrow]', function () {
  116. const $icon = $(this).find('i');
  117. const $textNode = $icon[0].nextSibling;
  118. if ($icon.hasClass('fa-arrow-up')) {
  119. treetable.foldAll(init.table_elem);
  120. $icon.removeClass('fa-arrow-up').addClass('fa-arrow-down');
  121. $textNode.textContent = ' 一键展开';
  122. $(this).attr('data-arrow', 'down');
  123. } else {
  124. treetable.expandAll(init.table_elem);
  125. $icon.removeClass('fa-arrow-down').addClass('fa-arrow-up');
  126. $textNode.textContent = ' 一键折叠';
  127. $(this).attr('data-arrow', 'up');
  128. }
  129. })
  130. ea.table.listenSwitch({filter: 'status', url: init.modify_url});
  131. ea.table.listenEdit(init, 'currentTable', init.table_render_id, true);
  132. ea.listen();
  133. },
  134. add: function () {
  135. $(function () {
  136. iconPickerFa.render({
  137. elem: '#icon',
  138. url: PATH_CONFIG.iconLess,
  139. limit: 12,
  140. click: function (data) {
  141. $('#icon').val('fa ' + data.icon);
  142. },
  143. success: function (d) {
  144. console.log(d);
  145. }
  146. });
  147. })
  148. autocomplete.render({
  149. elem: $('#href')[0],
  150. url: ea.url('system.menu/getMenuTips'),
  151. template_val: '{{d.node}}',
  152. template_txt: '{{d.node}} <span class=\'layui-badge layui-bg-gray\'>{{d.title}}</span>',
  153. onselect: function (resp) {
  154. }
  155. });
  156. ea.listen(function (data) {
  157. return data;
  158. }, function (res) {
  159. ea.msg.success(res.msg, function () {
  160. var index = parent.layer.getFrameIndex(window.name);
  161. parent.layer.close(index);
  162. parent.$('[data-treetable-refresh]').trigger("click");
  163. });
  164. });
  165. },
  166. edit: function () {
  167. $(function () {
  168. iconPickerFa.render({
  169. elem: '#icon',
  170. url: PATH_CONFIG.iconLess,
  171. limit: 12,
  172. click: function (data) {
  173. $('#icon').val('fa ' + data.icon);
  174. },
  175. success: function (d) {
  176. console.log(d);
  177. }
  178. });
  179. })
  180. autocomplete.render({
  181. elem: $('#href')[0],
  182. url: ea.url('system.menu/getMenuTips'),
  183. template_val: '{{d.node}}',
  184. template_txt: '{{d.node}} <span class=\'layui-badge layui-bg-gray\'>{{d.title}}</span>',
  185. onselect: function (resp) {
  186. }
  187. });
  188. ea.listen(function (data) {
  189. return data;
  190. }, function (res) {
  191. ea.msg.success(res.msg, function () {
  192. var index = parent.layer.getFrameIndex(window.name);
  193. parent.layer.close(index);
  194. parent.$('[data-treetable-refresh]').trigger("click");
  195. });
  196. });
  197. }
  198. };
  199. });