productSpecificationComponent.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <template>
  2. <div>
  3. <el-table
  4. :data="tableData"
  5. style="width: 100%"
  6. size="mini"
  7. border>
  8. <el-table-column
  9. prop="specification"
  10. align="center"
  11. label="产品规格型号">
  12. </el-table-column>
  13. <el-table-column
  14. prop="num"
  15. align="center"
  16. label="数量">
  17. </el-table-column>
  18. <el-table-column
  19. prop="batchNo"
  20. align="center"
  21. label="批号">
  22. </el-table-column>
  23. <el-table-column
  24. label="操作"
  25. align="center"
  26. width="100"
  27. v-if="!view">
  28. <template slot-scope="scope">
  29. <el-button @click="editRow(scope.row, scope.$index)" type="text" size="mini">编辑</el-button>
  30. <el-button @click="deleteRow(scope.$index)" type="text" size="mini">删除</el-button>
  31. </template>
  32. </el-table-column>
  33. </el-table>
  34. <div v-if="!view" @click="addRow" style="text-align: center;">
  35. <el-button type="text" icon="el-icon-plus">添加</el-button>
  36. </div>
  37. <ele-modal
  38. title="明细"
  39. custom-class="ele-dialog-form long-dialog-form"
  40. :visible.sync="visible"
  41. :before-close="handleClose"
  42. :close-on-click-modal="false"
  43. top="5vh"
  44. :close-on-press-escape="false"
  45. append-to-body
  46. width="70%"
  47. :maxable="true"
  48. >
  49. <el-card shadow="never">
  50. <el-form ref="form" :model="form">
  51. <el-form-item
  52. label="产品规格型号"
  53. label-width="110px"
  54. prop="specification"
  55. :rules="{
  56. required: true,
  57. message: '输入产品规格型号',
  58. trigger: 'change'
  59. }"
  60. >
  61. <el-input
  62. v-model="form.specification"
  63. placeholder="请输入"
  64. ></el-input>
  65. </el-form-item>
  66. <el-form-item
  67. label="数量"
  68. label-width="110px"
  69. prop="num"
  70. :rules="{
  71. required: true,
  72. message: '输入数量',
  73. trigger: 'change'
  74. }"
  75. >
  76. <el-input
  77. v-model="form.num"
  78. placeholder="请输入"
  79. ></el-input>
  80. </el-form-item>
  81. <el-form-item
  82. label="批号"
  83. label-width="110px"
  84. prop="batchNo"
  85. :rules="{
  86. required: true,
  87. message: '输入批号',
  88. trigger: 'change'
  89. }"
  90. >
  91. <el-input
  92. v-model="form.batchNo"
  93. placeholder="请输入"
  94. ></el-input>
  95. </el-form-item>
  96. </el-form>
  97. </el-card>
  98. <div slot="footer">
  99. <el-button type="primary" size="small" @click="save">保存</el-button>
  100. <el-button size="small" @click="handleClose">关闭</el-button>
  101. </div>
  102. </ele-modal>
  103. </div>
  104. </template>
  105. <script>
  106. export default {
  107. props: {
  108. info: {
  109. type: Object,
  110. default: () => {}
  111. },
  112. generateForm: {
  113. type: Object,
  114. default: () => {}
  115. },
  116. id: {
  117. type: String,
  118. default: ''
  119. },
  120. view: {
  121. type: Boolean,
  122. default: false
  123. },
  124. },
  125. watch: {
  126. info: {
  127. handler(newVal, oldVal) {
  128. console.log('info', newVal);
  129. this.tableData = newVal[this.id] || [];
  130. },
  131. deep: true,
  132. immediate: true
  133. },
  134. },
  135. data() {
  136. const formItem = {
  137. specification: '',
  138. num: '',
  139. batchNo: '',
  140. }
  141. return {
  142. form: {},
  143. formItem,
  144. useSealTypeList: [
  145. {
  146. value: '加盖',
  147. label: '加盖'
  148. },
  149. {
  150. value: '借用',
  151. label: '借用'
  152. },
  153. {
  154. value: '无',
  155. label: '无'
  156. },
  157. ],
  158. useSealNameList: [
  159. {
  160. value: '总公司公章',
  161. label: '总公司公章'
  162. },
  163. {
  164. value: '合同专用章',
  165. label: '合同专用章'
  166. },
  167. {
  168. value: '法人章',
  169. label: '法人章'
  170. },
  171. {
  172. value: '财务专用章',
  173. label: '财务专用章'
  174. },
  175. {
  176. value: '发票专用章',
  177. label: '发票专用章'
  178. },
  179. {
  180. value: '质检专用章',
  181. label: '质检专用章'
  182. },
  183. {
  184. value: '业务专用章',
  185. label: '业务专用章'
  186. },
  187. {
  188. value: '出库专用章',
  189. label: '出库专用章'
  190. },
  191. {
  192. value: '无',
  193. label: '无'
  194. },
  195. ],
  196. tableData: [],
  197. visible: false,
  198. currentItem: {},
  199. currentIndex: null,
  200. type: 'add'
  201. }
  202. },
  203. mounted() {
  204. // console.log('info~~~', this.info);
  205. // this.initForm();
  206. },
  207. methods: {
  208. async updateRowInfo() {
  209. let data = await this.generateForm.getData(false);
  210. this.generateForm.setData({
  211. [this.id]: this.tableData
  212. });
  213. console.log('updateRowInfo', this.tableData, data);
  214. },
  215. validateForm() {
  216. //开始表单校验
  217. return new Promise((resolve, reject) => {
  218. this.$refs.form.validate((valid) => {
  219. if (!valid) {
  220. reject(false);
  221. } else {
  222. resolve(true);
  223. }
  224. });
  225. });
  226. },
  227. addRow() {
  228. this.type = 'add';
  229. this.form = JSON.parse(JSON.stringify(this.formItem));
  230. this.visible = true;
  231. },
  232. handleClose() {
  233. this.visible = false;
  234. },
  235. editRow(row, index) {
  236. this.type = 'edit';
  237. console.log('编辑行', row, index);
  238. this.currentItem = row;
  239. this.currentIndex = index;
  240. this.visible = true;
  241. this.form = JSON.parse(JSON.stringify(row));
  242. },
  243. deleteRow(index) {
  244. this.tableData.splice(index, 1);
  245. this.updateRowInfo();
  246. },
  247. async save() {
  248. try {
  249. await this.validateForm();
  250. console.log('表单验证通过');
  251. // 深拷贝form对象,避免引用问题
  252. const newForm = JSON.parse(JSON.stringify(this.form));
  253. console.log('newForm', newForm);
  254. if(this.type === 'add') {
  255. this.tableData.push(newForm);
  256. } else {
  257. this.$set(this.tableData, this.currentIndex, newForm);
  258. }
  259. console.log('this.tableData', this.tableData);
  260. this.updateRowInfo()
  261. this.handleClose(); // 保存后关闭弹窗
  262. // 重置form
  263. this.form = {};
  264. } catch (error) {
  265. console.log('表单验证失败', error);
  266. }
  267. }
  268. }
  269. }
  270. </script>
  271. <style scoped>
  272. .header {
  273. display: flex;
  274. align-items: center;
  275. justify-content: space-between;
  276. width: 100%;
  277. }
  278. .blank_business_component {
  279. display: flex;
  280. align-items: center;
  281. margin-bottom: 10px;
  282. }
  283. .label {
  284. width: 80px;
  285. }
  286. .value {
  287. flex: 1;
  288. width: calc(100% - 80px);
  289. }
  290. :deep(.el-form-box .el-table .el-table__row .el-table__cell .cell) {
  291. padding-left: 10px;
  292. padding-right: 10px;
  293. }
  294. </style>