rule.js 6.2 KB

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