rule.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function ($, undefined, Backend, Table, Form, Template) {
  2. var Controller = {
  3. index: function () {
  4. // 初始化表格参数配置
  5. Table.api.init({
  6. extend: {
  7. "index_url": "auth/rule/index",
  8. "add_url": "auth/rule/add",
  9. "edit_url": "auth/rule/edit",
  10. "del_url": "auth/rule/del",
  11. "multi_url": "auth/rule/multi",
  12. "table": "auth_rule"
  13. }
  14. });
  15. var table = $("#table");
  16. // 初始化表格
  17. table.bootstrapTable({
  18. url: $.fn.bootstrapTable.defaults.extend.index_url,
  19. sortName: 'weigh',
  20. escape:false,
  21. columns: [
  22. [
  23. {field: 'state', checkbox: true, },
  24. {field: 'id', title: 'ID'},
  25. {field: 'title', title: __('Title'), align: 'left', formatter: Controller.api.formatter.title},
  26. {field: 'icon', title: __('Icon'), formatter: Controller.api.formatter.icon},
  27. {field: 'name', title: __('Name'), align: 'left', formatter: Controller.api.formatter.name},
  28. {field: 'weigh', title: __('Weigh')},
  29. {field: 'status', title: __('Status'), formatter: Table.api.formatter.status},
  30. {field: 'ismenu', title: __('Ismenu'), align: 'center', formatter: Controller.api.formatter.menu},
  31. {field: 'id', title: '<a href="javascript:;" class="btn btn-success btn-xs btn-toggle"><i class="fa fa-chevron-up"></i></a>', operate: false, formatter: Controller.api.formatter.subnode},
  32. {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
  33. ]
  34. ],
  35. pagination: false,
  36. search: false,
  37. commonSearch: false,
  38. });
  39. // 为表格绑定事件
  40. Table.api.bindevent(table);//当内容渲染完成后
  41. //默认隐藏所有子节点
  42. table.on('post-body.bs.table', function (e, settings, json, xhr) {
  43. //$("a.btn[data-id][data-pid][data-pid!=0]").closest("tr").hide();
  44. $(".btn-node-sub.disabled").closest("tr").hide();
  45. //显示隐藏子节点
  46. $(".btn-node-sub").off("click").on("click", function (e) {
  47. var status = $(this).data("shown") ? true : false;
  48. $("a.btn[data-pid='" + $(this).data("id") + "']").each(function () {
  49. $(this).closest("tr").toggle(!status);
  50. });
  51. $(this).data("shown", !status);
  52. return false;
  53. });
  54. });
  55. //展开隐藏一级
  56. $(document.body).on("click", ".btn-toggle", function (e) {
  57. $("a.btn[data-id][data-pid][data-pid!=0].disabled").closest("tr").hide();
  58. var that = this;
  59. var show = $("i", that).hasClass("fa-chevron-down");
  60. $("i", that).toggleClass("fa-chevron-down", !show);
  61. $("i", that).toggleClass("fa-chevron-up", show);
  62. $("a.btn[data-id][data-pid][data-pid!=0]").not('.disabled').closest("tr").toggle(show);
  63. $(".btn-node-sub[data-pid=0]").data("shown", show);
  64. });
  65. //展开隐藏全部
  66. $(document.body).on("click", ".btn-toggle-all", function (e) {
  67. var that = this;
  68. var show = $("i", that).hasClass("fa-plus");
  69. $("i", that).toggleClass("fa-plus", !show);
  70. $("i", that).toggleClass("fa-minus", show);
  71. $(".btn-node-sub.disabled").closest("tr").toggle(show);
  72. $(".btn-node-sub").data("shown", show);
  73. });
  74. },
  75. add: function () {
  76. Controller.api.bindevent();
  77. },
  78. edit: function () {
  79. Controller.api.bindevent();
  80. },
  81. api: {
  82. formatter: {
  83. title: function (value, row, index) {
  84. return !row.ismenu ? "<span class='text-muted'>" + value + "</span>" : value;
  85. },
  86. name: function (value, row, index) {
  87. return !row.ismenu ? "<span class='text-muted'>" + value + "</span>" : value;
  88. },
  89. menu: function (value, row, index) {
  90. return "<a href='javascript:;' class='btn btn-" + (value ? "info" : "default") + " btn-xs btn-change' data-id='"
  91. + row.id + "' data-params='ismenu=" + (value ? 0 : 1) + "'>" + (value ? __('Yes') : __('No')) + "</a>";
  92. },
  93. icon: function (value, row, index) {
  94. return '<i class="' + value + '"></i>';
  95. },
  96. subnode: function (value, row, index) {
  97. return '<a href="javascript:;" data-id="' + row['id'] + '" data-pid="' + row['pid'] + '" class="btn btn-xs '
  98. + (row['haschild'] == 1 ? 'btn-success' : 'btn-default disabled') + ' btn-node-sub"><i class="fa fa-sitemap"></i></a>';
  99. }
  100. },
  101. bindevent: function () {
  102. $(document).on('click', "input[name='row[ismenu]']", function () {
  103. var name = $("input[name='row[name]']");
  104. name.prop("placeholder", $(this).val() == 1 ? name.data("placeholder-menu") : name.data("placeholder-node"));
  105. });
  106. $("input[name='row[ismenu]']:checked").trigger("click");
  107. var iconlist = [];
  108. Form.api.bindevent($("form[role=form]"));
  109. $(document).on('click', ".btn-search-icon", function () {
  110. if (iconlist.length == 0) {
  111. $.get(Config.site.cdnurl + "/assets/libs/font-awesome/less/variables.less", function (ret) {
  112. var exp = /fa-var-(.*):/ig;
  113. var result;
  114. while ((result = exp.exec(ret)) != null) {
  115. iconlist.push(result[1]);
  116. }
  117. Layer.open({
  118. type: 1,
  119. area: ['460px', '300px'], //宽高
  120. content: Template('chooseicontpl', {iconlist: iconlist})
  121. });
  122. });
  123. } else {
  124. Layer.open({
  125. type: 1,
  126. area: ['460px', '300px'], //宽高
  127. content: Template('chooseicontpl', {iconlist: iconlist})
  128. });
  129. }
  130. });
  131. $(document).on('click', '#chooseicon ul li', function () {
  132. $("input[name='row[icon]']").val('fa fa-' + $(this).data("font"));
  133. Layer.closeAll();
  134. });
  135. $(document).on('keyup', 'input.js-icon-search', function () {
  136. $("#chooseicon ul li").show();
  137. if ($(this).val() != '') {
  138. $("#chooseicon ul li:not([data-font*='" + $(this).val() + "'])").hide();
  139. }
  140. });
  141. }
  142. }
  143. };
  144. return Controller;
  145. });