menu.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. $(function () {
  2. initialPage();
  3. getGrid();
  4. });
  5. function initialPage() {
  6. $(window).resize(function () {
  7. TreeGrid.table.resetHeight({height: $(window).height() - 100});
  8. });
  9. }
  10. function getGrid() {
  11. var colunms = TreeGrid.initColumn();
  12. var table = new TreeTable(TreeGrid.id, '../sys/menu/queryAll', colunms);
  13. table.setExpandColumn(2);
  14. table.setIdField("menuId");
  15. table.setCodeField("menuId");
  16. table.setParentCodeField("parentId");
  17. table.setExpandAll(false);
  18. table.setHeight($(window).height() - 100);
  19. table.init();
  20. TreeGrid.table = table;
  21. }
  22. var TreeGrid = {
  23. id: "jqGrid",
  24. table: null,
  25. layerIndex: -1
  26. };
  27. /**
  28. * 初始化表格的列
  29. */
  30. TreeGrid.initColumn = function () {
  31. var columns = [
  32. {field: 'selectItem', radio: true},
  33. {title: '编号', field: 'menuId', visitable: false, align: 'center', valign: 'middle', width: '80px'},
  34. {title: '名称', field: 'name', align: 'center', valign: 'middle', width: '180px'},
  35. {title: '上级菜单', field: 'parentName', align: 'center', valign: 'middle', width: '100px'},
  36. {
  37. title: '图标',
  38. field: 'icon',
  39. align: 'center',
  40. valign: 'middle',
  41. width: '50px',
  42. formatter: function (item, index) {
  43. return item.icon == null ? '' : '<i class="' + item.icon + ' fa-lg"></i>';
  44. }
  45. },
  46. {
  47. title: '类型',
  48. field: 'type',
  49. align: 'center',
  50. valign: 'middle',
  51. width: '60px',
  52. formatter: function (item) {
  53. if (item.type === 0) {
  54. return '<span class="label label-primary">目录</span>';
  55. }
  56. if (item.type === 1) {
  57. return '<span class="label label-success">菜单</span>';
  58. }
  59. if (item.type === 2) {
  60. return '<span class="label label-warning">按钮</span>';
  61. }
  62. }
  63. },
  64. {title: '排序', field: 'orderNum', align: 'center', valign: 'middle', width: '50px'},
  65. {title: '菜单URL', field: 'url', align: 'center', valign: 'middle', width: '200px'},
  66. {title: '授权标识', field: 'perms', align: 'center', valign: 'middle'},
  67. {
  68. title: '状态', field: 'status', align: 'center', valign: 'middle', width: '80px',
  69. formatter: function (item) {
  70. if (item.status === 1) {
  71. return '<span class="label label-danger">无效</span>';
  72. }
  73. return '<span class="label label-success">有效</span>';
  74. }
  75. }];
  76. return columns;
  77. };
  78. var setting = {
  79. data: {
  80. simpleData: {
  81. enable: true,
  82. idKey: "menuId",
  83. pIdKey: "parentId",
  84. rootPId: -1
  85. },
  86. key: {
  87. url: "nourl"
  88. }
  89. }
  90. };
  91. var ztree;
  92. var vm = new Vue({
  93. el: '#rrapp',
  94. data: {
  95. showList: true,
  96. title: null,
  97. menu: {
  98. parentName: null,
  99. parentId: 0,
  100. type: 1,
  101. orderNum: 0,
  102. status: 0
  103. },
  104. q: {
  105. menuName: '',
  106. parentName: ''
  107. },
  108. ruleValidate: {
  109. name: [
  110. {required: true, message: '菜单名称不能为空', trigger: 'blur'}
  111. ],
  112. url: [
  113. {required: true, message: '菜单url不能为空', trigger: 'blur'}
  114. ],
  115. parentName: [
  116. {required: true, message: '上级菜单不能为空', trigger: 'blur'}
  117. ]
  118. }
  119. },
  120. methods: {
  121. selectIcon: function () {
  122. openWindow({
  123. type: 2,
  124. title: '选取图标',
  125. area: ['1030px', '500px'],
  126. content: ['icon.html'],
  127. btn: ['确定', '取消'],
  128. btn1: function (index, layero) {
  129. var icon = layero.find("iframe")[0].contentWindow.$("#icon").val();
  130. vm.menu.icon = icon;
  131. top.layer.close(index);
  132. layer.close(index);
  133. }
  134. });
  135. },
  136. getMenu: function (menuId) {
  137. //加载菜单树
  138. Ajax.request({
  139. url: "../sys/menu/select",
  140. async: true,
  141. successCallback: function (r) {
  142. ztree = $.fn.zTree.init($("#menuTree"), setting, r.menuList);
  143. var node = ztree.getNodeByParam("menuId", vm.menu.parentId);
  144. if (node) {
  145. ztree.selectNode(node);
  146. vm.menu.parentName = node.name;
  147. } else {
  148. node = ztree.getNodeByParam("menuId", 0);
  149. ztree.selectNode(node);
  150. vm.menu.parentName = node.name;
  151. }
  152. }
  153. });
  154. },
  155. add: function () {
  156. vm.showList = false;
  157. vm.title = "新增";
  158. var menuId = TreeGrid.table.getSelectedRow();
  159. var parentId = 0;
  160. if (menuId.length != 0) {
  161. parentId = menuId[0].id;
  162. }
  163. vm.menu = {parentName: null, parentId: parentId, type: 1, orderNum: 0, status: 0};
  164. vm.getMenu();
  165. },
  166. update: function (event) {
  167. var menuId = TreeGrid.table.getSelectedRow();
  168. if (menuId.length == 0) {
  169. alert("请选择一条记录");
  170. return;
  171. }
  172. Ajax.request({
  173. url: "../sys/menu/info/" + menuId[0].id,
  174. async: true,
  175. successCallback: function (r) {
  176. vm.showList = false;
  177. vm.title = "修改";
  178. vm.menu = r.menu;
  179. vm.getMenu();
  180. }
  181. });
  182. },
  183. del: function (event) {
  184. var menuIds = TreeGrid.table.getSelectedRow(), ids = [];
  185. if (menuIds.length == 0) {
  186. iview.Message.error("");
  187. return;
  188. }
  189. confirm('确定要删除选中的记录?', function () {
  190. $.each(menuIds, function (idx, item) {
  191. ids[idx] = item.id;
  192. });
  193. Ajax.request({
  194. url: "../sys/menu/delete",
  195. params: JSON.stringify(ids),
  196. contentType: "application/json",
  197. type: 'POST',
  198. successCallback: function () {
  199. alert('操作成功', function (index) {
  200. vm.reload();
  201. });
  202. }
  203. });
  204. });
  205. },
  206. saveOrUpdate: function (event) {
  207. var url = vm.menu.menuId == null ? "../sys/menu/save" : "../sys/menu/update";
  208. Ajax.request({
  209. url: url,
  210. params: JSON.stringify(vm.menu),
  211. contentType: "application/json",
  212. type: 'POST',
  213. successCallback: function () {
  214. alert('操作成功', function (index) {
  215. vm.reload();
  216. });
  217. }
  218. });
  219. },
  220. menuTree: function () {
  221. openWindow({
  222. title: "选择菜单",
  223. area: ['300px', '450px'],
  224. content: jQuery("#menuLayer"),
  225. btn: ['确定', '取消'],
  226. btn1: function (index) {
  227. var node = ztree.getSelectedNodes();
  228. //选择上级菜单
  229. vm.menu.parentId = node[0].menuId;
  230. vm.menu.parentName = node[0].name;
  231. layer.close(index);
  232. }
  233. });
  234. },
  235. query: function () {
  236. vm.reload();
  237. },
  238. reload: function (event) {
  239. vm.showList = true;
  240. TreeGrid.table.refresh();
  241. },
  242. handleSubmit: function (name) {
  243. handleSubmitValidate(this, name, function () {
  244. vm.saveOrUpdate()
  245. });
  246. },
  247. handleReset: function (name) {
  248. handleResetForm(this, name);
  249. }
  250. }
  251. });