sku_template.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
  2. var vm;
  3. var Controller = {
  4. index: function () {
  5. // 初始化表格参数配置
  6. Table.api.init({
  7. extend: {
  8. index_url: 'shop/sku_template/index' + location.search,
  9. add_url: 'shop/sku_template/add',
  10. edit_url: 'shop/sku_template/edit',
  11. del_url: 'shop/sku_template/del',
  12. multi_url: 'shop/sku_template/multi',
  13. import_url: 'shop/sku_template/import',
  14. table: 'shop_sku_template',
  15. }
  16. });
  17. var table = $("#table");
  18. // 初始化表格
  19. table.bootstrapTable({
  20. url: $.fn.bootstrapTable.defaults.extend.index_url,
  21. pk: 'id',
  22. sortName: 'id',
  23. columns: [
  24. [{
  25. checkbox: true
  26. },
  27. {
  28. field: 'id',
  29. title: __('Id')
  30. },
  31. {
  32. field: 'name',
  33. title: __('Name'),
  34. operate: 'LIKE'
  35. },
  36. {
  37. field: 'spec_names',
  38. title: __('Spec_names'),
  39. operate: 'LIKE'
  40. },
  41. {
  42. field: 'spec_values',
  43. title: __('Spec_values'),
  44. operate: 'LIKE'
  45. },
  46. {
  47. field: 'createtime',
  48. title: __('Createtime'),
  49. operate: 'RANGE',
  50. addclass: 'datetimerange',
  51. autocomplete: false,
  52. formatter: Table.api.formatter.datetime
  53. },
  54. {
  55. field: 'updatetime',
  56. title: __('Updatetime'),
  57. operate: 'RANGE',
  58. addclass: 'datetimerange',
  59. autocomplete: false,
  60. formatter: Table.api.formatter.datetime
  61. },
  62. {
  63. field: 'operate',
  64. title: __('Operate'),
  65. table: table,
  66. events: Table.api.events.operate,
  67. formatter: Table.api.formatter.operate
  68. }
  69. ]
  70. ]
  71. });
  72. // 为表格绑定事件
  73. Table.api.bindevent(table);
  74. },
  75. vue(url, callback = ()=>{}) {
  76. require(['vue'], function (Vue) {
  77. vm = new Vue({
  78. el: '#app',
  79. data() {
  80. return {
  81. spec_name: '',
  82. specList: [],
  83. }
  84. },
  85. mounted(){
  86. this.$nextTick(callback);
  87. },
  88. methods: {
  89. addSpec() {
  90. if (!this.spec_name.trim()) {
  91. Toastr.error('请输入规格名称');
  92. return;
  93. }
  94. if (this.specList.some(item => item.name == this.spec_name)) {
  95. Toastr.error('已存在规格名称');
  96. return;
  97. }
  98. this.specList.push({
  99. name: this.spec_name,
  100. value: []
  101. });
  102. this.spec_name = '';
  103. },
  104. addSpecValue(key) {
  105. if(this.specList[key] && this.specList[key].value.indexOf());
  106. this.specList[key].value.push('');
  107. },
  108. removeSpecValue(key, index) {
  109. this.specList[key].value.splice(index, 1);
  110. },
  111. removeSpec(key) {
  112. this.specList.splice(key, 1);
  113. },
  114. }
  115. })
  116. });
  117. //提交数据
  118. $(document).on('click', '.btn-add-sku', function () {
  119. if (vm.specList.length) {
  120. for (let item of vm.specList) {
  121. for (let val of item.value) {
  122. if (!val.trim()) {
  123. Toastr.error('请输入' + item.name + '的属性值');
  124. return;
  125. }
  126. }
  127. }
  128. }
  129. let name = $('#c-name').val();
  130. if (!name.trim()) {
  131. Toastr.error('请输入模板名称');
  132. return;
  133. }
  134. $.post(url, {
  135. spec: vm.specList,
  136. name: name
  137. }, function (res) {
  138. if (res.code == 1) {
  139. parent.Toastr.success(res.msg);
  140. var index = parent.Layer.getFrameIndex(window.name);
  141. parent.Layer.close(index);
  142. parent.$("#table").bootstrapTable('refresh', {});
  143. } else {
  144. Toastr.error(res.msg);
  145. }
  146. });
  147. });
  148. },
  149. add: function () {
  150. this.vue('shop/sku_template/add');
  151. Controller.api.bindevent();
  152. },
  153. edit: function () {
  154. $('#c-name').val(Config.row.name);
  155. this.vue('shop/sku_template/edit/ids/' + Config.row.id,function(){
  156. let spec_names = Config.row.spec_names.split(';');
  157. let spec_values = Config.row.spec_values.split(';');
  158. let list = [];
  159. for(let [i,v] of spec_names.entries()){
  160. list.push({
  161. name: v,
  162. value: spec_values[i].split(',')
  163. });
  164. }
  165. vm.specList = list;
  166. });
  167. Controller.api.bindevent();
  168. },
  169. api: {
  170. bindevent: function () {
  171. Form.api.bindevent($("form[role=form]"));
  172. }
  173. }
  174. };
  175. return Controller;
  176. });