当前位置:   article > 正文

Vue+antd做一个批量新增的动态表单(增加/删除表单)_antdesign vue2 如何在一个弹窗中做动态增减form表单

antdesign vue2 如何在一个弹窗中做动态增减form表单
  1. <template>
  2. <div class="addDevice">
  3. <div class="deviceContent">
  4. <a-page-header
  5. class="titleHeader"
  6. :title="$t('AddEquip')"
  7. :sub-title="$t('AddEquip1')"
  8. />
  9. <div class="device">
  10. <a-form-model v-for="(form, index) in deviceForm" :key="index" :rules="rules" :model="form" :ref="'form'+index" class="deviceForm" :label-col="{ span: 3 }" :wrapper-col="{ span: 6 }">
  11. <a-button type="primary" class="deleteBtn" @click="deleteDevice(index)">{{ $t('deletes') }}</a-button>
  12. <a-form-model-item :label="$t('EquipmentType')" prop="device_id" class="setSelect">
  13. <a-select v-model="form.device_id" placeholder="请选择设备类型">
  14. <a-select-option v-for="(item, index) in deviceTypeList" :key="index" :value="item.device_id">{{ item.device_name }}</a-select-option>
  15. </a-select>
  16. </a-form-model-item>
  17. <a-form-model-item :label="$t('DeviceName')" prop="name">
  18. <a-input v-model="form.name" :placeholder="$t('PleaseEnterName')"/>
  19. </a-form-model-item>
  20. <a-form-model-item :label="$t('EnglishEquipment')" prop="name_en">
  21. <a-input v-model="form.name_en" :placeholder="$t('PleaseEnglishName')"/>
  22. </a-form-model-item>
  23. <a-form-model-item :label="$t('DonglePoint')" prop="use_dongle_divide">
  24. <a-radio-group v-model="form.use_dongle_divide" v-decorator="['radio-group']">
  25. <a-radio value="1">{{ $t('yes') }}</a-radio>
  26. <a-radio value="0">{{ $t('no') }}</a-radio>
  27. </a-radio-group>
  28. </a-form-model-item>
  29. <a-form-model-item :label="$t('ThirdParty')" prop="is_third">
  30. <a-radio-group v-model="form.is_third" v-decorator="['radio-group']">
  31. <a-radio value="1">{{ $t('yes') }}</a-radio>
  32. <a-radio value="0">{{ $t('no') }}</a-radio>
  33. </a-radio-group>
  34. </a-form-model-item>
  35. <a-form-model-item :label="$t('remarks')">
  36. <a-textarea v-model="form.remark" :rows="4" :placeholder="$t('PleaseComments')"></a-textarea>
  37. </a-form-model-item>
  38. </a-form-model>
  39. <div class="btn">
  40. <a-button type="primary" :loading="loading" @click="applyCheck">{{ $t('apply') }}</a-button>
  41. <a-button type="primary" @click="addDevice" style="margin-left: 30px;">{{ $t('AddEquipment') }}</a-button>
  42. </div>
  43. </div>
  44. </div>
  45. </div>
  46. </template>
  47. <script>
  48. import { addMachines, getMachinesSimpleList } from '@/api/device'
  49. export default {
  50. name: "addDevice",
  51. i18n: require('../i18n'),
  52. data() {
  53. return {
  54. key: '',
  55. loading: false,
  56. resultValidateList: [],
  57. deviceTypeList: [],
  58. deviceForm: [
  59. {
  60. name: '',
  61. name_en: '',
  62. device_id: undefined,
  63. use_dongle_divide: '',
  64. is_third: '',
  65. remark: ''
  66. }
  67. ],
  68. rules: {
  69. name: [
  70. { required: true, message: '请填写设备中文名!', trigger: 'blur' }
  71. ],
  72. name_en: [
  73. { required: true, message: '请填写设备英文名!', trigger: 'blur' }
  74. ],
  75. device_id: [
  76. { required: true, message: '请选择设备类型!', trigger: 'change' }
  77. ],
  78. use_dongle_divide: [
  79. { required: true, message: '加密狗是否扣点!', trigger: 'change' }
  80. ],
  81. is_third: [
  82. { required: true, message: '是否第三方设备!', trigger: 'change' }
  83. ]
  84. }
  85. }
  86. },
  87. methods: {
  88. // 创建设备
  89. addMachines() {
  90. const _this = this
  91. this.loading = true
  92. const deviceArray = JSON.parse(JSON.stringify(this.deviceForm))
  93. deviceArray.forEach((item) => {
  94. item.device_id = Number(item.device_id)
  95. item.use_dongle_divide = Number(item.use_dongle_divide)
  96. item.is_third = Number(item.is_third)
  97. })
  98. const params = {
  99. operator_id: Number(this.$route.query.id),
  100. apply_id: 0,
  101. machines: deviceArray
  102. }
  103. addMachines(params, this.key).then(response => {
  104. this.loading = false
  105. const { code, msg } = response.data
  106. if (code === 1) {
  107. this.$message.success(msg, 3)
  108. setTimeout(function() {
  109. _this.$closePage(_this.$route.path, '/store/list')
  110. }, 500)
  111. }
  112. }).catch(() => {
  113. this.loading = false
  114. })
  115. },
  116. // 删除设备
  117. deleteDevice(index) {
  118. this.deviceForm.splice(index, 1)
  119. },
  120. // 提交表单
  121. applyCheck() {
  122. this.resultValidateList = []
  123. for(let i = 0; i < this.deviceForm.length; i++){
  124. this.checkValidate('form' + i)
  125. }
  126. Promise.all(this.resultValidateList).then(() => {
  127. // 提交数据
  128. this.addMachines()
  129. })
  130. },
  131. // 表单验证
  132. checkValidate(name) {
  133. const that = this
  134. const result = new Promise((resolve, reject) => {
  135. that.$refs[name][0].validate(valid => {
  136. if (valid) {
  137. resolve(true)
  138. } else {
  139. reject(false)
  140. }
  141. })
  142. })
  143. this.resultValidateList.push(result)
  144. },
  145. // 增加设备
  146. addDevice() {
  147. if (this.deviceForm.length < 10 ) {
  148. const arr = {
  149. name: '',
  150. name_en: '',
  151. device_id: undefined,
  152. use_dongle_divide: '',
  153. is_third: '',
  154. remark: ''
  155. }
  156. this.deviceForm.push(arr)
  157. } else {
  158. this.$message.warn('最多一次新增10个设备', 3)
  159. }
  160. },
  161. // 获取设备类型
  162. getMachinesSimpleList() {
  163. const params = {
  164. page: 0,
  165. limit: 0,
  166. status: 1
  167. }
  168. getMachinesSimpleList(params, this.key).then(response => {
  169. const { data } = response.data
  170. this.deviceTypeList = data.list
  171. })
  172. },
  173. },
  174. created() {
  175. // 路由key
  176. this.key = this.$route.meta.key
  177. // 获取设备类型
  178. this.getMachinesSimpleList()
  179. },
  180. }
  181. </script>
  182. <style lang="less" scoped>
  183. .addDevice{
  184. background: #fff;
  185. padding: 30px;
  186. min-height: 750px;
  187. .deviceContent{
  188. .titleHeader{
  189. padding-bottom: 40px;
  190. }
  191. .device{
  192. .deviceForm{
  193. position: relative;
  194. border-bottom: 1px solid #e4e3e3;
  195. padding-bottom: 25px;
  196. margin-bottom: 40px;
  197. .deleteBtn{
  198. position: absolute;
  199. top: 0;
  200. right: 47%;
  201. cursor: pointer;
  202. z-index: 9;
  203. }
  204. }
  205. }
  206. .btn{
  207. margin-left: 160px;
  208. }
  209. .more{
  210. cursor: pointer;
  211. padding-left: 21%;
  212. text-align: left;
  213. margin-bottom: 30px;
  214. color: rgba(0, 0, 0, 0.85);
  215. }
  216. }
  217. }
  218. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/91550
推荐阅读
相关标签
  

闽ICP备14008679号