rule.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
  2. var Controller = {
  3. index: function () {
  4. // 初始化表格参数配置
  5. Table.api.init({
  6. extend: {
  7. index_url: 'user/rule/index',
  8. add_url: 'user/rule/add',
  9. edit_url: 'user/rule/edit',
  10. del_url: 'user/rule/del',
  11. multi_url: 'user/rule/multi',
  12. table: 'user_rule',
  13. }
  14. });
  15. var table = $("#table");
  16. // 初始化表格
  17. table.bootstrapTable({
  18. url: $.fn.bootstrapTable.defaults.extend.index_url,
  19. pk: 'id',
  20. sortName: 'weigh',
  21. escape: false,
  22. columns: [
  23. [
  24. {checkbox: true},
  25. {field: 'id', title: __('Id')},
  26. {field: 'pid', title: __('Pid'), visible: false},
  27. {field: 'title', title: __('Title'), align: 'left', formatter: Controller.api.formatter.title},
  28. {field: 'name', title: __('Name'), align: 'left', formatter: Controller.api.formatter.name},
  29. {field: 'remark', title: __('Remark')},
  30. // {field: 'ismenu', title: __('Ismenu'), formatter: Table.api.formatter.toggle},
  31. {field: 'createtime', title: __('Createtime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true, visible: false},
  32. {field: 'updatetime', title: __('Updatetime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true, visible: false},
  33. {field: 'weigh', title: __('Weigh')},
  34. {field: 'status', title: __('Status'), formatter: Table.api.formatter.status},
  35. {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
  36. ]
  37. ],
  38. pagination: false,
  39. search: false,
  40. commonSearch: false,
  41. rowAttributes: function (row, index) {
  42. return row.pid == 0 ? {} : {style: "display:none"};
  43. }
  44. });
  45. // 为表格绑定事件
  46. Table.api.bindevent(table);
  47. table.on('post-body.bs.table', function (e, settings, json, xhr) {
  48. //显示隐藏子节点
  49. $(">tbody>tr[data-index] > td", this).on('click', "a.btn-node-sub", function () {
  50. var status = $(this).data("shown") ? true : false;
  51. $("a[data-pid='" + $(this).data("id") + "']").each(function () {
  52. $(this).closest("tr").toggle(!status);
  53. });
  54. if (status) {
  55. $("a[data-pid='" + $(this).data("id") + "']").trigger("collapse");
  56. }
  57. $(this).data("shown", !status);
  58. $("i", this).toggleClass("fa-caret-down").toggleClass("fa-caret-right");
  59. return false;
  60. });
  61. });
  62. //隐藏子节点
  63. $(document).on("collapse", ".btn-node-sub", function () {
  64. if ($("i", this).length > 0) {
  65. $("a[data-pid='" + $(this).data("id") + "']").trigger("collapse");
  66. }
  67. $("i", this).removeClass("fa-caret-down").addClass("fa-caret-right");
  68. $(this).data("shown", false);
  69. $(this).closest("tr").toggle(false);
  70. });
  71. //展开隐藏一级
  72. $(document.body).on("click", ".btn-toggle", function (e) {
  73. $("a[data-id][data-pid][data-pid!=0].disabled").closest("tr").hide();
  74. var that = this;
  75. var show = $("i", that).hasClass("fa-chevron-down");
  76. $("i", that).toggleClass("fa-chevron-down", !show).toggleClass("fa-chevron-up", show);
  77. $("a[data-id][data-pid][data-pid!=0]").not('.disabled').closest("tr").toggle(show);
  78. $(".btn-node-sub[data-pid=0]").data("shown", show);
  79. });
  80. //展开隐藏全部
  81. $(document.body).on("click", ".btn-toggle-all", function (e) {
  82. var that = this;
  83. var show = $("i", that).hasClass("fa-plus");
  84. $("i", that).toggleClass("fa-plus", !show).toggleClass("fa-minus", show);
  85. $(".btn-node-sub:not([data-pid=0])").closest("tr").toggle(show);
  86. $(".btn-node-sub").data("shown", show);
  87. $(".btn-node-sub > i").toggleClass("fa-caret-down", show).toggleClass("fa-caret-right", !show);
  88. });
  89. },
  90. add: function () {
  91. Controller.api.bindevent();
  92. },
  93. edit: function () {
  94. Controller.api.bindevent();
  95. },
  96. api: {
  97. formatter: {
  98. title: function (value, row, index) {
  99. value = value.toString().replace(/(&|&)nbsp;/g, ' ');
  100. var caret = row.haschild == 1 || row.ismenu == 1 ? '<i class="fa fa-caret-right"></i>' : '';
  101. value = value.indexOf("&nbsp;") > -1 ? value.replace(/(.*)&nbsp;/, "$1" + caret) : caret + value;
  102. value = !row.ismenu || row.status == 'hidden' ? "<span class='text-muted'>" + value + "</span>" : value;
  103. return '<a href="javascript:;" data-id="' + row.id + '" data-pid="' + row.pid + '" class="'
  104. + (row.haschild == 1 || row.ismenu == 1 ? 'text-primary' : 'disabled') + ' btn-node-sub">' + value + '</a>';
  105. },
  106. name: function (value, row, index) {
  107. return !row.ismenu || row.status == 'hidden' ? "<span class='text-muted'>" + value + "</span>" : value;
  108. },
  109. },
  110. bindevent: function () {
  111. $(document).on('click', "input[name='row[ismenu]']", function () {
  112. var name = $("input[name='row[name]']");
  113. name.prop("placeholder", $(this).val() == 1 ? name.data("placeholder-menu") : name.data("placeholder-node"));
  114. });
  115. $("input[name='row[ismenu]']:checked").trigger("click");
  116. Form.api.bindevent($("form[role=form]"));
  117. }
  118. }
  119. };
  120. return Controller;
  121. });