add.html 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <template>
  2. <el-card shadow="never" style="min-height: 500px;">
  3. <el-tabs tab-position="left" style="height: 400px;" v-model="tabValue" @tab-click="handleClick">
  4. <el-tab-pane v-for="(item,index) in tabData" :label="item.label" :name="item.value">
  5. </el-tab-pane>
  6. <el-form ref="form" :model="form" label-width="0" stripe>
  7. <el-form-item>
  8. <el-table :data="tableData" style="width: 100%" border :ref="'multipleTable_' + tabValue" stripe
  9. @selection-change="handleSelectionChange">
  10. <el-table-column type="selection" width="55">
  11. </el-table-column>
  12. <el-table-column prop="name" label="规格" align="center">
  13. </el-table-column>
  14. <el-table-column prop="packet" label="包装箱" align="center">
  15. <template #default="{row}">
  16. <el-select v-model="row.packet" placeholder="请选择">
  17. <el-option v-for="item in options" :key="item.value" :label="item.label"
  18. :value="item.value">
  19. </el-option>
  20. </el-select>
  21. </template>
  22. </el-table-column>
  23. <el-table-column prop="price" label="发货价" align="center">
  24. <template #default="{row}">
  25. <el-input v-model="row.price"></el-input>
  26. </template>
  27. </el-table-column>
  28. </el-table>
  29. </el-form-item>
  30. <el-form-item style="padding-top: 20px;">
  31. <el-button type="primary" @click="onSubmit">立即创建</el-button>
  32. <el-button>取消</el-button>
  33. </el-form-item>
  34. </el-form>
  35. </el-tabs>
  36. </el-card>
  37. </template>
  38. <script>
  39. import table from "@components/Table.js";
  40. export default {
  41. components: { 'YunTable': table },
  42. data: {
  43. extend: {
  44. index_url: 'general/category/index',
  45. },
  46. row: Yunqi.data.row,
  47. tabData: Object.entries(Yunqi.data.typeList).map(([value, label]) => ({ label, value })),
  48. tableData: [],
  49. value: "",
  50. form: {
  51. nameValue: null,
  52. },
  53. options: Object.entries(Yunqi.data.packingList).map(([value, label]) => ({ label, value: value.toString() })),//品种
  54. tabValue: Object.entries(Yunqi.data.typeList).map(([value, label]) => ({ label, value }))[0].value, //选中的品种 获取到品种数据后要给他默认赋值第一个
  55. chooseSpec: [], // 当前分类选中的规格
  56. allData: {},
  57. type: 1, // 1编辑 0新增
  58. },
  59. methods: {
  60. onSubmit() {
  61. if (this.allData == "") return;
  62. let arr = Object.values(this.allData).flat(Infinity)
  63. console.log(JSON.stringify(arr), "所有数据")
  64. //
  65. Yunqi.ajax.post('shop/customer_spec/add', { ids: Yunqi.data.ids, all_data: arr }, false, false, true).then(res => {
  66. if (res.code == 200) {
  67. this.$message.success(__('设置成功'));
  68. Yunqi.api.closelayer(Yunqi.app.window.id, true);
  69. } else {
  70. Yunqi.alert(__('请填写发货价格'), __('温馨提示'), { type: 'error' });
  71. }
  72. });
  73. },
  74. handleSelectionChange(e) {
  75. if (e.length > 0) {
  76. this.allData[this.tabValue] = e || []
  77. }
  78. },
  79. handleClick(data, row) {
  80. // 品种切换事件 可以在这里请求规格接口
  81. this.tabValue = data.props.name
  82. // this.chooseSpec = this.allData[this.tabValue]
  83. console.log(this.allData[this.tabValue], "this.allData[this.tabValue]")
  84. this.tabName = data.props.label
  85. this.tableData = [];
  86. this.getSpecData()
  87. },
  88. getSpecData() {
  89. //获取规格
  90. Yunqi.ajax.get('shop/customer_spec/get_box', { type_id: this.tabValue }, false, false, true).then(res => {
  91. //编辑数据
  92. if (res.length > 0) {
  93. console.log("res", res)
  94. for (var key in Object(res)) {
  95. let index = this.tableData.findIndex(item => item.value == res[key]['id']);
  96. if (index != -1) {
  97. this.tableData.splice(index, 1)
  98. } else {
  99. this.tableData.push({ name: res[key]['title'], value: res[key]['id'], price: res[key]['price'], type_id: this.tabValue });
  100. }
  101. }
  102. this.$nextTick(() => {
  103. const tableRef = this.$refs['multipleTable_' + this.tabValue];
  104. if (this.type == 0 && this.allData[this.tabValue]?.length > 0 && tableRef) {
  105. this.tableData.forEach((item, index) => {
  106. const matched = this.allData[this.tabValue].find(row => {
  107. return row.value == item.value
  108. });
  109. if (matched) {
  110. this.tableData[index] = matched
  111. }
  112. })
  113. this.allData[this.tabValue].forEach(selected => {
  114. const matched = this.tableData.find(row => row.value === selected.value);
  115. if (matched) {
  116. this.$refs['multipleTable_' + this.tabValue].toggleRowSelection(matched, true); // 勾选
  117. }
  118. });
  119. }
  120. else if (this.type == 1 && this.allData[Number(this.tabValue)]) {
  121. this.tableData.forEach((item, index) => {
  122. const matched = this.allData[Number(this.tabValue)].find(row => {
  123. if (row.product_id) {
  124. return row.product_id == item.value
  125. } else {
  126. return row.value == item.value
  127. }
  128. });
  129. console.log(matched, "type2matched");
  130. if (matched) {
  131. // this.tableData[index] = { name: 'kam', value: 3, price: '111', type_id: '4', packet: '3' }
  132. if (matched.product_id) {
  133. this.tableData[index].price = matched.price
  134. this.tableData[index].packet = matched.box_id != 0 ? matched?.box_id.toString() : ""
  135. } else {
  136. this.tableData[index] = matched
  137. }
  138. }
  139. })
  140. this.allData[Number(this.tabValue)].forEach(selected => {
  141. const matched = this.tableData.find(row => {
  142. if (selected.product_id) {
  143. return selected.product_id == row.value
  144. } else {
  145. return row.value === selected.value
  146. }
  147. });
  148. if (matched) {
  149. this.$refs['multipleTable_' + this.tabValue].toggleRowSelection(matched, true); // 勾选
  150. }
  151. });
  152. // this.type = 0
  153. }
  154. });
  155. }
  156. });
  157. }
  158. },
  159. onLoad: function (query) {
  160. let row = Yunqi.data.row;
  161. if (row.length > 0) {
  162. this.type = 1
  163. } else {
  164. this.type = 0
  165. }
  166. console.log(this.type)
  167. const grouped = row.reduce((acc, item) => {
  168. const key = item.type_id;
  169. if (!acc[key]) {
  170. acc[key] = [];
  171. }
  172. console.log(item)
  173. item.value = item.product_id
  174. item.packet = item.box_id
  175. acc[key].push(item);
  176. return acc;
  177. }, {});
  178. this.allData = JSON.parse(JSON.stringify(grouped))
  179. this.getSpecData()
  180. },
  181. }
  182. </script>
  183. <style>
  184. </style>