PackageGoodsTable.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <template>
  2. <div>
  3. <el-table :data="setList" ref="table">
  4. <el-table-column v-if="multipleMode" align="center" type="selection" width="50"> </el-table-column>
  5. <el-table-column prop="name" label="名称" min-width="120px"> </el-table-column>
  6. <el-table-column prop="num" label="数量"> </el-table-column>
  7. <el-table-column prop="unit" label="单位"> </el-table-column>
  8. <el-table-column prop="price" label="价格"> </el-table-column>
  9. <el-table-column prop="remark" label="备注" min-width="100px"> </el-table-column>
  10. <el-table-column label="操作" align="center" fixed="right" min-width="150">
  11. <template slot-scope="{ row }">
  12. <el-button @click="editRow(row)" type="primary" size="mini" plain>编辑</el-button>
  13. <el-button @click="deleteRow(row)" type="danger" size="mini" plain>删除</el-button>
  14. </template>
  15. </el-table-column>
  16. </el-table>
  17. <div class="pagination-wrapper">
  18. <el-button @click="editRow()" type="primary">添加</el-button>
  19. <el-button @click="$router.go(-1)">取消</el-button>
  20. </div>
  21. <el-dialog :visible.sync="showDialog" width="500px" title="套餐内商品" :close-on-click-modal="false">
  22. <el-form :model="formData" :rules="rules" ref="form" label-width="80px" label-position="right" size="small">
  23. <el-form-item prop="goodsInfoId" label="名称">
  24. <el-select v-model="formData.goodsInfoId" filterable @change="optionChange" class="cl-input">
  25. <el-option v-for="item in goodsList" :key="item.id" :value="item.id" :label="item.name">
  26. <span style="float: left">{{ item.name }}</span>
  27. <span style="float: right; color: #8492a6; font-size: 13px">{{ item.unit }}</span>
  28. </el-option>
  29. </el-select>
  30. </el-form-item>
  31. <el-form-item prop="num" label="数量">
  32. <el-input-number type="number" v-model="formData.num" class="cl-input" :min="1"></el-input-number>
  33. </el-form-item>
  34. <el-form-item prop="remark" label="备注">
  35. <el-input v-model="formData.remark" class="cl-input"></el-input>
  36. </el-form-item>
  37. </el-form>
  38. <span slot="footer">
  39. <el-button type="primary" size="mini" @click="save" :loading="saving">保存</el-button>
  40. </span>
  41. </el-dialog>
  42. </div>
  43. </template>
  44. <script>
  45. export default {
  46. name: 'PackageGoodsTable',
  47. data() {
  48. return {
  49. multipleMode: false,
  50. search: '',
  51. id: '',
  52. setList: [],
  53. showDialog: false,
  54. formData: {},
  55. saving: false,
  56. rules: {
  57. goodsInfoId: [
  58. {
  59. required: true,
  60. message: '请选择商品',
  61. trigger: 'blur'
  62. }
  63. ],
  64. num: [
  65. {
  66. required: true,
  67. message: '请输入数量',
  68. trigger: 'blur'
  69. }
  70. ]
  71. },
  72. goodsList: [],
  73. goods: {}
  74. };
  75. },
  76. computed: {
  77. selection() {
  78. return this.$refs.table.selection.map(i => i.id);
  79. }
  80. },
  81. mounted() {
  82. this.$http
  83. .post('goodsInfo/allList')
  84. .then(res => {
  85. this.goodsList = res;
  86. })
  87. .catch(e => {
  88. console.log(e);
  89. this.$message.error(e.error);
  90. });
  91. },
  92. methods: {
  93. /*beforeGetData() {
  94. return { search: this.search };
  95. },*/
  96. toggleMultipleMode(multipleMode) {
  97. this.multipleMode = multipleMode;
  98. if (!multipleMode) {
  99. this.$refs.table.clearSelection();
  100. }
  101. },
  102. addRow() {
  103. this.$router.push({
  104. path: '/packageGoodsEdit',
  105. query: {
  106. ...this.$route.query
  107. }
  108. });
  109. },
  110. editRow(row, edit) {
  111. this.edit = edit;
  112. if (!row) {
  113. row = {
  114. name: '',
  115. price: ''
  116. };
  117. }
  118. this.formData = { ...row };
  119. this.showDialog = true;
  120. },
  121. download() {
  122. this.downloading = true;
  123. this.$axios
  124. .get('/packageGoods/excel', {
  125. responseType: 'blob',
  126. params: { size: 10000 }
  127. })
  128. .then(res => {
  129. console.log(res);
  130. this.downloading = false;
  131. const downloadUrl = window.URL.createObjectURL(new Blob([res.data]));
  132. const link = document.createElement('a');
  133. link.href = downloadUrl;
  134. link.setAttribute('download', res.headers['content-disposition'].split('filename=')[1]);
  135. document.body.appendChild(link);
  136. link.click();
  137. link.remove();
  138. })
  139. .catch(e => {
  140. console.log(e);
  141. this.downloading = false;
  142. this.$message.error(e.error);
  143. });
  144. },
  145. operation1() {
  146. this.$notify({
  147. title: '提示',
  148. message: this.selection
  149. });
  150. },
  151. operation2() {
  152. this.$message('操作2');
  153. },
  154. deleteRow(row) {
  155. this.$alert('删除将无法恢复,确认要删除么?', '警告', { type: 'error' })
  156. .then(() => {
  157. return this.$http.post(`/packageGoods/del/${row.id}`);
  158. })
  159. .then(() => {
  160. this.$message.success('删除成功');
  161. this.$router.go(0);
  162. })
  163. .catch(e => {
  164. if (e !== 'cancel') {
  165. this.$message.error(e.error);
  166. }
  167. });
  168. },
  169. getList(id) {
  170. if (id) {
  171. this.id = id;
  172. }
  173. this.$http
  174. .post('/packageGoods/getSetGoods', {
  175. packageId: this.id
  176. })
  177. .then(res => {
  178. this.setList = res;
  179. })
  180. .catch(e => {
  181. console.log(e);
  182. });
  183. },
  184. save() {
  185. this.$refs.form.validate(valid => {
  186. if (valid) {
  187. let data = { ...this.formData };
  188. data.price = this.goods.price * data.num;
  189. data.name = this.goods.name;
  190. data.unit = this.goods.unit;
  191. data.packageId = this.id;
  192. this.saving = true;
  193. this.$http
  194. .post('/packageGoods/save', data, { body: 'json' })
  195. .then(res => {
  196. this.saving = false;
  197. this.$message.success('成功');
  198. this.showDialog = false;
  199. this.$router.go(0);
  200. })
  201. .catch(e => {
  202. console.log(e);
  203. this.saving = false;
  204. this.showDialog = false;
  205. this.$message.error(e.error);
  206. });
  207. } else {
  208. return false;
  209. }
  210. });
  211. },
  212. optionChange(id) {
  213. this.goods = this.goodsList.find(item => {
  214. return item.id === id;
  215. });
  216. }
  217. }
  218. };
  219. </script>
  220. <style lang="less" scoped>
  221. .cl-input {
  222. width: 300px;
  223. }
  224. </style>