Step2.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <template>
  2. <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
  3. <a-table
  4. :columns="columns"
  5. :dataSource="model.data"
  6. :pagination="false"
  7. :rowKey="(record, index) => { return index }"
  8. bordered
  9. class="u-edit-table"
  10. >
  11. <!-- <template
  12. v-for="(col, i) in ['sampleName', 'sampleModelSpecification', 'sampleQuantities','sampleManufacturer']"
  13. :slot="col" slot-scope="text, record">
  14. <a-input
  15. :disabled="formDisabled"
  16. :key="col"
  17. style="margin: -5px 0"
  18. :value="text"
  19. :placeholder="columns[i].title"
  20. @change="e => handleChange(e.target.value, record.key, col)"
  21. />
  22. </template> -->
  23. <template slot="sampleName" slot-scope="text, record,index">
  24. <a-form-model-item :prop="'data.'+index+'.sampleName'" :rules="validatorRules.sampleName">
  25. <a-input :disabled="formDisabled" v-model="record.sampleName"></a-input>
  26. </a-form-model-item>
  27. </template>
  28. <template slot="sampleModelSpecification" slot-scope="text, record,index">
  29. <a-form-model-item :prop="'data.'+index+'.sampleModelSpecification'"
  30. :rules="validatorRules.sampleModelSpecification">
  31. <a-input :disabled="formDisabled" v-model="record.sampleModelSpecification"></a-input>
  32. </a-form-model-item>
  33. </template>
  34. <template slot="sampleQuantities" slot-scope="text, record,index">
  35. <a-form-model-item :prop="'data.'+index+'.sampleQuantities'" :rules="validatorRules.sampleQuantities">
  36. <a-input :disabled="formDisabled" v-model="record.sampleQuantities"></a-input>
  37. </a-form-model-item>
  38. </template>
  39. <template slot="sampleManufacturer" slot-scope="text, record,index">
  40. <a-form-model-item :prop="'data.'+index+'.sampleManufacturer'" :rules="validatorRules.sampleManufacturer">
  41. <a-input :disabled="formDisabled" v-model="record.sampleManufacturer"></a-input>
  42. </a-form-model-item>
  43. </template>
  44. <template slot="operation" slot-scope="text, record">
  45. <span>
  46. <a-popconfirm title="是否要删除此行?" :disabled="formDisabled" @confirm="remove(record.key)">
  47. <a>删除</a>
  48. </a-popconfirm>
  49. </span>
  50. </template>
  51. </a-table>
  52. <a-button style="width: 100%; margin-top: 16px; margin-bottom: 8px" type="dashed" :disabled="formDisabled"
  53. icon="plus" @click="newMember">
  54. 新增样品
  55. </a-button>
  56. <a-form-item class="buttonAll">
  57. <div class="all">
  58. <a-button @click="prevStep" class="next">上一步</a-button>
  59. <a-button type="primary" @click="nextStep" class="next">下一步</a-button>
  60. </div>
  61. </a-form-item>
  62. </a-form-model>
  63. </template>
  64. <script>
  65. export default {
  66. name: 'step2',
  67. props: {
  68. //表单禁用
  69. disabled: {
  70. type: Boolean,
  71. default: false,
  72. required: false
  73. }
  74. },
  75. data() {
  76. let checkSampleNameGgxh = (rule, value, callback) => {
  77. // console.log(rule, value, callback)
  78. var currentRowIndex = parseInt(rule.field.substring(5, 6))
  79. var currentValue = this.model.data[currentRowIndex]
  80. // console.log(this.model.data[currentRowIndex])
  81. var arr = this.model.data.filter(item => item.sampleName === currentValue.sampleName && item.sampleModelSpecification === currentValue.sampleModelSpecification)
  82. // console.log(arr)
  83. if (arr.length > 1) {
  84. callback(new Error('样品名称和规格型号不能全重复'))
  85. } else {
  86. callback()
  87. }
  88. }
  89. let numbermin = (rule, value, callback) => {
  90. if (value < 1) {
  91. callback(new Error('样品数量必须大于等于1'))
  92. } else {
  93. callback()
  94. }
  95. }
  96. return {
  97. count: 1,
  98. validatorRules: {
  99. sampleName: [
  100. { required: true, message: '请输入样品名称!' },
  101. { validator: checkSampleNameGgxh }
  102. ],
  103. sampleModelSpecification: [
  104. { required: true, message: '请输入样品规格/型号!' },
  105. { validator: checkSampleNameGgxh }
  106. ],
  107. sampleQuantities: [
  108. { required: true, message: '请输入样品数量!' },
  109. { type: 'number', message: '样品数量必须是数字',transform:(value)=>Number(value) },
  110. { validator: numbermin }
  111. ],
  112. sampleManufacturer: [
  113. { required: true, message: '请输入生产厂家!' }
  114. ]
  115. },
  116. model: {
  117. data: []
  118. },
  119. description: '高级表单常见于一次性输入和提交大批量数据的场景。',
  120. loading: false,
  121. // table
  122. columns: [
  123. {
  124. title: '样品名称',
  125. dataIndex: 'sampleName',
  126. key: 'sampleName',
  127. align: 'center',
  128. scopedSlots: { customRender: 'sampleName' }
  129. },
  130. {
  131. title: '样品规格/型号',
  132. dataIndex: 'sampleModelSpecification',
  133. key: 'sampleModelSpecification',
  134. align: 'center',
  135. scopedSlots: { customRender: 'sampleModelSpecification' }
  136. },
  137. {
  138. title: '样品数量',
  139. dataIndex: 'sampleQuantities',
  140. key: 'sampleQuantities',
  141. align: 'center',
  142. scopedSlots: { customRender: 'sampleQuantities' }
  143. },
  144. {
  145. title: '生产厂家',
  146. dataIndex: 'sampleManufacturer',
  147. key: 'sampleManufacturer',
  148. align: 'center',
  149. scopedSlots: { customRender: 'sampleManufacturer' }
  150. },
  151. {
  152. title: '操作',
  153. key: 'action',
  154. align: 'center',
  155. scopedSlots: { customRender: 'operation' }
  156. }
  157. ]
  158. }
  159. },
  160. computed: {
  161. formDisabled() {
  162. return this.disabled
  163. },
  164. baseInfos() {
  165. return this.$store.getters.baseInfos
  166. },
  167. yangpinInfos() {
  168. return this.$store.getters.yangpinInfos
  169. },
  170. },
  171. created() {
  172. this.setInitData()
  173. },
  174. methods: {
  175. // 设置初始数据
  176. setInitData(){
  177. if (this.yangpinInfos.length > 0) {
  178. this.model.data = this.yangpinInfos.map((item, index) => {
  179. item.key = (index+1) + ''
  180. item.editable = true
  181. return item
  182. })
  183. this.count = this.yangpinInfos.length + 1
  184. // console.log(this.model.data)
  185. } else {
  186. this.model.data = []
  187. this.newMember()
  188. }
  189. },
  190. // 新增样品
  191. newMember() {
  192. console.log(this.count)
  193. this.model.data.push({
  194. key: this.count + '',
  195. sampleQuantities: 1,
  196. sampleModelSpecification: '',
  197. sampleName: '',
  198. sampleManufacturer: this.baseInfos ? this.baseInfos.weituoClient : '',
  199. editable: true,
  200. isNew: true
  201. })
  202. this.count++
  203. },
  204. // 删除
  205. remove(key) {
  206. console.log(key)
  207. const newData = this.model.data.filter(item => item.key !== key)
  208. this.model.data = newData
  209. // this.model.data.splice(this.model.data.findIndex(item=>item.key === key), 1)
  210. },
  211. //saveRow(key) {
  212. // let target = this.data.filter(item => item.key === key)[0]
  213. // target.editable = false
  214. // target.isNew = false
  215. // },
  216. handleChange(value, key, column) {
  217. const newData = [...this.model.data]
  218. const target = newData.filter(item => key === item.key)[0]
  219. if (target) {
  220. target[column] = value
  221. this.model.data = newData
  222. }
  223. },
  224. // 下一步
  225. nextStep() {
  226. // console.log(11111,this.model.data)
  227. this.$refs.form.validate(valid => {
  228. if (valid) {
  229. this.$store.commit('SET_YANGPININFO', this.model.data);
  230. this.$emit('nextStep')
  231. }
  232. })
  233. },
  234. // 上一步
  235. prevStep() {
  236. this.$store.commit('SET_YANGPININFO', this.model.data);
  237. this.$emit('prevStep')
  238. },
  239. }
  240. }
  241. </script>
  242. <style lang="less" scoped>
  243. .u-edit-table{
  244. margin-top: 12px;
  245. .ant-form-item{
  246. margin-bottom: 0;
  247. }
  248. /deep/.ant-table-tbody .ant-table-row td {
  249. padding-top: 2px !important;
  250. padding-bottom: 2px !important;
  251. }
  252. // .ant-table-tbody > tr > td{
  253. // padding-top: 0 !important;
  254. // }
  255. .ant-input{
  256. // padding: 0;
  257. border: none;
  258. }
  259. .ant-table-tbody > tr:hover:not(.ant-table-expanded-row):not(.ant-table-row-selected) .ant-input{
  260. background: #e6f7ff;
  261. }
  262. // .ant-input:hover{
  263. // background: #e6f7ff;
  264. // }
  265. .ant-input:focus{
  266. box-shadow: none;
  267. }
  268. }
  269. .card {
  270. margin-bottom: 24px;
  271. }
  272. .next {
  273. width: 35%;
  274. margin-left: 20px;
  275. margin-right: 20px;
  276. margin-top: 20px;
  277. }
  278. .buttonAll {
  279. width: 100%;
  280. align-items: center;
  281. justify-content: center;
  282. }
  283. .all {
  284. width: 100%;
  285. display: flex;
  286. align-items: center;
  287. justify-content: center;
  288. }
  289. </style>