group.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'jstree'], function ($, undefined, Backend, Table, Form, undefined) {
  2. //读取选中的条目
  3. $.jstree.core.prototype.get_all_checked = function (full) {
  4. var obj = this.get_selected(), i, j;
  5. for (i = 0, j = obj.length; i < j; i++) {
  6. obj = obj.concat(this.get_node(obj[i]).parents);
  7. }
  8. obj = $.grep(obj, function (v, i, a) {
  9. return v != '#';
  10. });
  11. obj = obj.filter(function (itm, i, a) {
  12. return i == a.indexOf(itm);
  13. });
  14. return full ? $.map(obj, $.proxy(function (i) {
  15. return this.get_node(i);
  16. }, this)) : obj;
  17. };
  18. var Controller = {
  19. index: function () {
  20. // 初始化表格参数配置
  21. Table.api.init({
  22. extend: {
  23. "index_url": "qingdongams/department/group/index",
  24. "add_url": "qingdongams/department/group/add",
  25. "edit_url": "qingdongams/department/group/edit",
  26. "del_url": "qingdongams/department/group/del",
  27. "multi_url": "qingdongams/department/group/multi",
  28. }
  29. });
  30. var table = $("#table");
  31. //在表格内容渲染完成后回调的事件
  32. table.on('post-body.bs.table', function (e, json) {
  33. $("tbody tr[data-index]", this).each(function () {
  34. if (Config.admin.group_ids.indexOf(parseInt(parseInt($("td:eq(1)", this).text()))) > -1) {
  35. $("input[type=checkbox]", this).prop("disabled", true);
  36. }
  37. });
  38. });
  39. // 初始化表格
  40. table.bootstrapTable({
  41. url: $.fn.bootstrapTable.defaults.extend.index_url,
  42. columns: [
  43. [
  44. {field: 'state', checkbox: true,},
  45. {field: 'id', title: 'ID'},
  46. {field: 'pid', title: __('Parent')},
  47. {field: 'name', title: __('Name'), align: 'left', formatter:function (value, row, index) {
  48. return value.toString().replace(/(&|&amp;)nbsp;/g, '&nbsp;');
  49. }
  50. },
  51. {field: 'status', title: __('Status'), formatter: Table.api.formatter.status},
  52. {
  53. field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: function (value, row, index) {
  54. if (Config.admin.group_ids.indexOf(parseInt(row.id)) > -1) {
  55. return '';
  56. }
  57. return Table.api.formatter.operate.call(this, value, row, index);
  58. }
  59. }
  60. ]
  61. ],
  62. pagination: false,
  63. search: false,
  64. commonSearch: false,
  65. onLoadSuccess:function(){
  66. // 这里就是数据渲染结束后的回调函数
  67. $('.btn-editone').html('编辑');
  68. $('.fa-pencil').remove();
  69. $('.btn-delone').html('删除');
  70. $('.fa-trash').remove();
  71. $('.btn-editone').removeClass('btn-success')
  72. $('.btn-editone').removeClass('btn')
  73. $('.btn-delone').removeClass('btn')
  74. $('.btn-delone').removeClass('btn-danger')
  75. }
  76. });
  77. // 为表格绑定事件
  78. Table.api.bindevent(table);//当内容渲染完成后
  79. $('#myPopover').popover();
  80. },
  81. add: function () {
  82. Controller.api.bindevent();
  83. },
  84. edit: function () {
  85. Controller.api.bindevent();
  86. },
  87. api: {
  88. bindevent: function () {
  89. Form.api.bindevent($("form[role=form]"), null, null, function () {
  90. if ($("#treeview").length > 0) {
  91. var r = $("#treeview").jstree("get_all_checked");
  92. $("input[name='row[rules]']").val(r.join(','));
  93. }
  94. return true;
  95. });
  96. //渲染权限节点树
  97. //变更级别后需要重建节点树
  98. $(document).on("change", "select[name='row[pid]']", function () {
  99. var pid = $(this).data("pid");
  100. var id = $(this).data("id");
  101. if ($(this).val() == id) {
  102. $("option[value='" + pid + "']", this).prop("selected", true).change();
  103. Backend.api.toastr.error(__('Can not change the parent to self'));
  104. return false;
  105. }
  106. $.ajax({
  107. url: "auth/group/roletree",
  108. type: 'post',
  109. dataType: 'json',
  110. data: {id: id, pid: $(this).val()},
  111. success: function (ret) {
  112. if (ret.hasOwnProperty("code")) {
  113. var data = ret.hasOwnProperty("data") && ret.data != "" ? ret.data : "";
  114. if (ret.code === 1) {
  115. //销毁已有的节点树
  116. $("#treeview").jstree("destroy");
  117. Controller.api.rendertree(data);
  118. } else {
  119. Backend.api.toastr.error(ret.msg);
  120. }
  121. }
  122. }, error: function (e) {
  123. Backend.api.toastr.error(e.message);
  124. }
  125. });
  126. });
  127. //全选和展开
  128. $(document).on("click", "#checkall", function () {
  129. $("#treeview").jstree($(this).prop("checked") ? "check_all" : "uncheck_all");
  130. });
  131. $(document).on("click", "#expandall", function () {
  132. $("#treeview").jstree($(this).prop("checked") ? "open_all" : "close_all");
  133. });
  134. $("select[name='row[pid]']").trigger("change");
  135. },
  136. rendertree: function (content) {
  137. $("#treeview")
  138. .on('redraw.jstree', function (e) {
  139. $(".layer-footer").attr("domrefresh", Math.random());
  140. })
  141. .jstree({
  142. "themes": {"stripes": true},
  143. "checkbox": {
  144. "keep_selected_style": false,
  145. },
  146. "types": {
  147. "root": {
  148. "icon": "fa fa-folder-open",
  149. },
  150. "menu": {
  151. "icon": "fa fa-folder-open",
  152. },
  153. "file": {
  154. "icon": "fa fa-file-o",
  155. }
  156. },
  157. "plugins": ["checkbox", "types"],
  158. "core": {
  159. 'check_callback': true,
  160. "data": content
  161. }
  162. });
  163. }
  164. }
  165. };
  166. return Controller;
  167. });