template.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
  2. let table1_extend = {
  3. index_url: 'template/template/publicParentIndex/' + location.search,
  4. add_url: 'template/template/publicAddParent',
  5. edit_url: 'template/template/publicEditParent',
  6. del_url: 'template/template/publicDelParent',
  7. multi_url: 'template/template/multi',
  8. table: 'templates',
  9. };
  10. let table2_extend = {
  11. index_url: 'template/template/publicChildIndex/' + location.search,
  12. add_url: 'template/template/publicAddChild',
  13. edit_url: 'template/template/publicEditChild',
  14. del_url: 'template/template/del',
  15. multi_url: 'template/template/multi',
  16. table: 'templates',
  17. };
  18. let Controller = {
  19. index: function () {
  20. // 初始化表格参数配置
  21. Table.api.init();
  22. this.table.first();
  23. this.table.second();
  24. },
  25. table: {
  26. first: function () {
  27. // 初始化表格参数配置
  28. Table.api.init({
  29. extend: table1_extend
  30. });
  31. let table1 = $("#table1");
  32. // 初始化表格
  33. table1.bootstrapTable({
  34. url: $.fn.bootstrapTable.defaults.extend.index_url,
  35. pk: 'id',
  36. pageSize: 10,
  37. toolbar: '#toolbar1',
  38. showToggle: false,
  39. showExport: false,
  40. showColumns: false,
  41. commonSearch: false,
  42. singleSelect: true, //是否启用单选
  43. clickToSelect: true,
  44. onCheck: function (rows) { // 选中事件
  45. // 刷新子级列表
  46. $('#child_name').text(rows.title);
  47. $('#selected_parent').data('pid',rows.id);
  48. $('#toolbar2 .btn-refresh').trigger('click');
  49. },
  50. columns: [
  51. [
  52. {checkbox: true,},
  53. {field: 'title', title: __('Title')},
  54. {field: 'exam_class_id', title: __('Exam_class_id')},
  55. {
  56. field: 'operate',
  57. title: __('Operate'),
  58. table: table1,
  59. events: Controller.api.events.table1_operate,
  60. formatter: Table.api.formatter.operate
  61. }
  62. ]
  63. ]
  64. });
  65. // 默认选中第一条
  66. table1.on('post-body.bs.table', function () {
  67. $('#table1 tr[data-index="0"] td input[data-index="0"]').trigger('click');
  68. });
  69. // 为表格绑定事件
  70. Table.api.bindevent(table1);
  71. // 自定义添加按钮事件
  72. $('#toolbar1 .btn-add-1').on('click', function () {
  73. let ids = Table.api.selectedids(table1);
  74. let url = table1_extend.add_url;
  75. if (url.indexOf("{ids}") !== -1) {
  76. url = Table.api.replaceurl(url, {ids: ids.length > 0 ? ids.join(",") : 0}, table1);
  77. }
  78. Fast.api.open(url, __('Add'), $(this).data() || {});
  79. });
  80. },
  81. second: function () {
  82. // 初始化表格参数配置
  83. Table.api.init({
  84. extend: table2_extend
  85. });
  86. let table2 = $("#table2");
  87. // 初始化表格
  88. table2.bootstrapTable({
  89. url: $.fn.bootstrapTable.defaults.extend.index_url,
  90. pk: 'id',
  91. toolbar: '#toolbar2',
  92. sortName: 'createdAt',
  93. showToggle: false,
  94. showColumns: false,
  95. queryParams: function queryParams(params) {
  96. params.pid = $('#selected_parent').data('pid');
  97. return params;
  98. },
  99. columns: [
  100. [
  101. {field: 'title', title: __('Title'), align: "left"},
  102. {field: 'exam_class_id', title: __('Exam_class_id'),searchable:false},
  103. {field: 'description', title: __('Description'), searchable:false, hover: true, formatter:Table.api.formatter.content, width: '200'},
  104. {field: 'impression', title: __('Impression'), searchable:false, hover: true, formatter:Table.api.formatter.content, width: '200'},
  105. {field: 'createdAt', title: __('Createdat'), operate: 'RANGE', addclass: 'datetimerange'},
  106. {
  107. field: 'operate',
  108. title: __('Operate'),
  109. table: table2,
  110. events: Table.api.events.operate,
  111. formatter: Table.api.formatter.operate
  112. }
  113. ]
  114. ]
  115. });
  116. // 为表格绑定事件
  117. Table.api.bindevent(table2);
  118. $('#toolbar2 .btn-add-2').on('click', function () {
  119. let pid = $('#selected_parent').data('pid');
  120. let url = table2_extend.add_url;
  121. url = Table.api.replaceurl(url, {ids: pid}, table2);
  122. Fast.api.open(url, __('Add'), $(this).data() || {});
  123. });
  124. return table2;
  125. },
  126. },
  127. publicaddparent: function () {
  128. Controller.api.bindevent();
  129. },
  130. publiceditparent: function () {
  131. Controller.api.bindevent();
  132. },
  133. publicaddchild: function () {
  134. Controller.api.bindevent();
  135. },
  136. publiceditchild: function () {
  137. Controller.api.bindevent();
  138. },
  139. api: {
  140. bindevent: function () {
  141. Form.api.bindevent($("form[role=form]"));
  142. },
  143. formatter: {
  144. name: function (value) {
  145. return "<strong>" + value + "</strong>";
  146. }
  147. },
  148. events: {
  149. table1_operate: {
  150. 'click .btn-editone': function (e, value, row, index) {
  151. e.stopPropagation();
  152. e.preventDefault();
  153. let table = $(this).closest('table');
  154. let options = table.bootstrapTable('getOptions');
  155. let ids = row[options.pk];
  156. row = $.extend({}, row ? row : {}, {ids: ids});
  157. let url = table1_extend.edit_url;
  158. Fast.api.open(Table.api.replaceurl(url, row, table), __('Edit'), $(this).data() || {});
  159. },
  160. 'click .btn-delone': function (e, value, row, index) {
  161. e.stopPropagation();
  162. e.preventDefault();
  163. let that = this;
  164. $(that).data('url',table1_extend.del_url);
  165. let top = $(that).offset().top - $(window).scrollTop();
  166. let left = $(that).offset().left - $(window).scrollLeft() - 260;
  167. if (top + 154 > $(window).height()) {
  168. top = top - 154;
  169. }
  170. if ($(window).width() < 480) {
  171. top = left = undefined;
  172. }
  173. Layer.confirm(
  174. __('Are you sure you want to delete this item?'),
  175. {icon: 3, title: __('Warning'), offset: [top, left], shadeClose: true},
  176. function (index) {
  177. let table = $(that).closest('table');
  178. let options = table.bootstrapTable('getOptions');
  179. Table.api.multi("del", row[options.pk], table, that);
  180. Layer.close(index);
  181. }
  182. );
  183. }
  184. }
  185. }
  186. },
  187. };
  188. return Controller;
  189. });