admin-util.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // ======================== 一些工具方法 ========================
  2. var sa_admin_code_util = {
  3. // 删除数组某个元素
  4. arrayDelete: function(arr, item){
  5. var index = arr.indexOf(item);
  6. if (index > -1) {
  7. arr.splice(index, 1);
  8. }
  9. },
  10. //执行一个函数, 解决layer拉伸或者最大化的时候,iframe高度不能自适应的问题
  11. solveLayerBug: function(index) {
  12. var selected = '#layui-layer' + index;
  13. var height = $(selected).height();
  14. var title_height = $(selected).find('.layui-layer-title').height();
  15. $(selected).find('iframe').css('height', (height - title_height) + 'px');
  16. // var selected = '#layui-layer' + index;
  17. // var height = $(selected).height();
  18. // var title_height = $(selected).find('.layui-layer-title').height();
  19. // $(selected).find('iframe').css('height', (height - title_height) + 'px');
  20. },
  21. // ======================== 菜单集合相关 ========================
  22. // 将一维平面数组转换为 Tree 菜单 (根据其指定的parent_id添加到其父菜单的childList)
  23. arrayToTree: function(menu_list) {
  24. for (var i = 0; i < menu_list.length; i++) {
  25. var menu = menu_list[i];
  26. // 添加到其指定的父菜单的childList
  27. if(menu.parent_id) {
  28. var parent_menu = this.findMenuById(menu_list, menu.parent_id);
  29. if(parent_menu) {
  30. parent_menu.childList = parent_menu.childList || [];
  31. parent_menu.childList.push(menu);
  32. menu_list.splice(i, 1); // 从一维中删除
  33. i--;
  34. }
  35. }
  36. }
  37. return menu_list;
  38. },
  39. // 将 menu_list 处理一下
  40. refMenuList: function(menu_list) {
  41. for (var i = 0; i < menu_list.length; i++) {
  42. var menu = menu_list[i];
  43. // 有子项的递归处理
  44. if(menu.childList){
  45. menu.children = menu.childList;
  46. this.refMenuList(menu.childList);
  47. }
  48. }
  49. return menu_list;
  50. },
  51. // 返回指定 index 的menu
  52. getMenuById: function(menuList, id) {
  53. for (var i = 0; i < menuList.length; i++) {
  54. var menu = menuList[i];
  55. if(menu.id + '' == id + '') {
  56. return menu;
  57. }
  58. // 如果是二级或多级
  59. if(menu.childList) {
  60. var menu2 = this.getMenuById(menu.childList, id);
  61. if(menu2 != null) {
  62. return menu2;
  63. }
  64. }
  65. }
  66. return null;
  67. },
  68. // 将 Tree 菜单 转换为 一维平面数组
  69. treeToArray: function(menu_list) {
  70. var arr = [];
  71. function _dg(menu_list) {
  72. menu_list = menu_list || [];
  73. for (var i = 0; i < menu_list.length; i++) {
  74. var menu = menu_list[i];
  75. arr.push(menu);
  76. // 如果有子菜单
  77. if(menu.childList) {
  78. _dg(menu.childList);
  79. }
  80. }
  81. }
  82. _dg(menu_list);
  83. return arr;
  84. },
  85. }