category.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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: 'category/index',
  8. add_url: 'category/add',
  9. edit_url: 'category/edit',
  10. del_url: 'category/del',
  11. multi_url: 'category/multi',
  12. dragsort_url: 'ajax/weigh',
  13. table: 'category',
  14. }
  15. });
  16. var table = $("#table");
  17. var tableOptions = {
  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: 'type', title: __('Type'), operate: false, searchList: Config.searchList, formatter: Table.api.formatter.label},
  27. {field: 'name', title: __('Name'), align: 'left', formatter:function (value, row, index) {
  28. return value.toString().replace(/(&|&)nbsp;/g, ' ');
  29. }
  30. },
  31. {field: 'nickname', title: __('Nickname')},
  32. // {field: 'flag', title: __('Flag'), formatter: Table.api.formatter.flag},
  33. // {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
  34. {field: 'weigh', title: __('Weigh')},
  35. {
  36. field: 'id',
  37. title: '<a href="javascript:;" class="btn btn-success btn-xs btn-toggle" style="border-top:none;"><i class="fa fa-chevron-up"></i></a>',
  38. operate: false,
  39. formatter: Controller.api.formatter.subnode
  40. },
  41. {field: 'status', title: __('Status'), operate: false, formatter: Table.api.formatter.status},
  42. {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
  43. ]
  44. ],
  45. pagination: false,
  46. search: false,
  47. commonSearch: false,
  48. rowAttributes: function (row, index) {
  49. if (this.totalRows > 500) {
  50. return row.pid == 0 ? {} : {style: "display:none"};
  51. }
  52. return row.haschild == 1 || row.ismenu == 1 ? {} : {style: "display:none"};
  53. }
  54. };
  55. // 初始化表格
  56. table.bootstrapTable(tableOptions);
  57. // 为表格绑定事件
  58. Table.api.bindevent(table);
  59. //绑定TAB事件
  60. // $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
  61. // // var options = table.bootstrapTable(tableOptions);
  62. // var typeStr = $(this).attr("href").replace('#', '');
  63. // var options = table.bootstrapTable('getOptions');
  64. // options.pageNumber = 1;
  65. // options.queryParams = function (params) {
  66. // // params.filter = JSON.stringify({type: typeStr});
  67. // params.type = typeStr;
  68. //
  69. // return params;
  70. // };
  71. // table.bootstrapTable('refresh', {});
  72. // return false;
  73. //
  74. // });
  75. //必须默认触发shown.bs.tab事件
  76. // $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
  77. var btnSuccessEvent = function (data, ret) {
  78. if ($(this).hasClass("btn-change")) {
  79. var index = $(this).data("index");
  80. var row = Table.api.getrowbyindex(table, index);
  81. row.ismenu = $("i.fa.text-gray", this).length > 0 ? 1 : 0;
  82. table.bootstrapTable("updateRow", {index: index, row: row});
  83. } else if ($(this).hasClass("btn-delone")) {
  84. if ($(this).closest("tr[data-index]").find("a.btn-node-sub.disabled").length > 0) {
  85. $(this).closest("tr[data-index]").remove();
  86. } else {
  87. table.bootstrapTable('refresh');
  88. }
  89. } else if ($(this).hasClass("btn-dragsort")) {
  90. table.bootstrapTable('refresh');
  91. }
  92. Fast.api.refreshmenu();
  93. return false;
  94. };
  95. //表格内容渲染前
  96. table.on('pre-body.bs.table', function (e, data) {
  97. var options = table.bootstrapTable("getOptions");
  98. options.escape = true;
  99. });
  100. //当内容渲染完成后
  101. table.on('post-body.bs.table', function (e, data) {
  102. var options = table.bootstrapTable("getOptions");
  103. options.escape = false;
  104. //点击切换/排序/删除操作后刷新左侧菜单
  105. $(".btn-change[data-id],.btn-delone,.btn-dragsort").data("success", btnSuccessEvent);
  106. });
  107. //显示隐藏子节点
  108. $(document).on("click", ".btn-node-sub", function (e) {
  109. var status = $(this).data("shown") ? true : false;
  110. $("a.btn[data-pid='" + $(this).data("id") + "']").each(function () {
  111. $(this).closest("tr").toggle(!status);
  112. });
  113. $(this).data("shown", !status);
  114. return false;
  115. });
  116. //批量删除后的回调
  117. $(".toolbar > .btn-del,.toolbar .btn-more~ul>li>a").data("success", function (e) {
  118. Fast.api.refreshmenu();
  119. });
  120. //展开隐藏一级
  121. $(document.body).on("click", ".btn-toggle", function (e) {
  122. $("a.btn[data-id][data-pid][data-pid!=0].disabled").closest("tr").hide();
  123. var that = this;
  124. var show = $("i", that).hasClass("fa-chevron-down");
  125. $("i", that).toggleClass("fa-chevron-down", !show);
  126. $("i", that).toggleClass("fa-chevron-up", show);
  127. $("a.btn[data-id][data-pid][data-pid!=0]").not('.disabled').closest("tr").toggle(show);
  128. $(".btn-node-sub[data-pid=0]").data("shown", show);
  129. });
  130. //展开隐藏全部
  131. $(document.body).on("click", ".btn-toggle-all", function (e) {
  132. var that = this;
  133. var show = $("i", that).hasClass("fa-plus");
  134. $("i", that).toggleClass("fa-plus", !show);
  135. $("i", that).toggleClass("fa-minus", show);
  136. $(".btn-node-sub.disabled").closest("tr").toggle(show);
  137. $(".btn-node-sub").data("shown", show);
  138. });
  139. },
  140. add: function () {
  141. Controller.api.bindevent();
  142. setTimeout(function () {
  143. $("#c-type").trigger("change");
  144. }, 100);
  145. },
  146. edit: function () {
  147. Controller.api.bindevent();
  148. },
  149. api: {
  150. formatter: {
  151. subnode: function (value, row, index) {
  152. return '<a href="javascript:;" data-toggle="tooltip" title="' + __('Toggle sub menu') + '" data-id="' + row.id + '" data-pid="' + row.pid + '" class="btn btn-xs '
  153. + (row.haschild == 1 || row.ismenu == 1 ? 'btn-success' : 'btn-default disabled') + ' btn-node-sub"><i class="fa fa-sitemap"></i></a>';
  154. }
  155. },
  156. bindevent: function () {
  157. $(document).on("change", "#c-type", function () {
  158. $("#c-pid option[data-type='all']").prop("selected", true);
  159. $("#c-pid option").removeClass("hide");
  160. $("#c-pid option[data-type!='" + $(this).val() + "'][data-type!='all']").addClass("hide");
  161. $("#c-pid").data("selectpicker") && $("#c-pid").selectpicker("refresh");
  162. });
  163. Form.api.bindevent($("form[role=form]"));
  164. }
  165. }
  166. };
  167. return Controller;
  168. });