当前位置:   article > 正文

antd vue中,如何给表单动态添加input,解决遇到一些坑

antd inputnumber 换成input 报错 filter

最近做项目用的组件库Ant Design Vue,这个组件看起来文档很详细,但也会遇到一些意想不到的问题,最后看文档没办法解决,只能去看源码,折腾了好久,终于把问题给解决了,遇到的问题主要是当添加input的时候验证不能友好的使用,当一个input添加,然后删除,提交表单的时候会提示填写删除的input,还有个问题就是当把填写好的内容,删除提交的时候,已删除的内容还是能提交到后台,用方法处理

  1. form.getFieldValue('email').splice(index, 1)
  2. form.getFieldValue('truename').splice(index, 1)
  3. form.setFieldsValue({
  4. keys: keys.filter(key => key !== index),
  5. email: form.getFieldValue('email'),
  6. truename: form.getFieldValue('truename')
  7. })
  8. 复制代码

这样设置的值的时候,数据还是没删除成功,还能提交到后台,后来才知道需要先注册 注册,这样setFieldsValue方法才能成功,但是添加了之后也没删除成功,

  1. this.form.getFieldDecorator('keys', { initialValue: [0], preserve: true })
  2. this.form.getFieldDecorator('email', { initialValue: [], preserve: false })
  3. this.form.getFieldDecorator('truename', { initialValue: [], preserve: false })
  4. 复制代码

经过看代码后来发现文档里面有个没提示到this.form.clearField(''),用这个方法总算解决了这个问题。 但是上面方法也有个不好的方法就是,keys在删除的时候不能按顺序排列,造成values数组里面也出现了空字段现象,需要重新处理数组,把字段设置成preserve: false。用对象数组循环数据,用循环的中的索引值组成keys,这样在删除,添加的时候,keys的索引跟values数组一直对应,values数组中不会出现空元素现象,在表单提交的时候加上需要验证的字段,这样问题就解决了,大致的代码如下

  1. <template>
  2. <div class="invite-new">
  3. <div class="invite-second-crumb">
  4. <second-crumb />
  5. </div>
  6. <div class="invite-box">
  7. <h2>通过邮件邀请,无需审核</h2>
  8. <p>发送邀请给指定的邮箱,对方只需进行个人账户设置即可直接加入团队</p>
  9. <div class="invite-list">
  10. <a-form
  11. ref="inviteForm"
  12. layout="inline"
  13. :form="form"
  14. class="demo-form-inline"
  15. @submit.prevent="submitHandle('inviteForm')"
  16. >
  17. <div
  18. v-for="(item,index) in formData"
  19. :key="index"
  20. class="invite-item"
  21. >
  22. <a-form-item
  23. :required="false"
  24. >
  25. <div class="input_email">
  26. <a-input
  27. v-decorator="[
  28. `email[${index}]`,
  29. {
  30. validateTrigger: [ 'blur'],
  31. rules: [{
  32. required: true,
  33. whitespace: true,
  34. message: '请输入邮箱'
  35. },
  36. {
  37. validator: isEmailRe
  38. },
  39. {
  40. validator:isErrorEmail
  41. },
  42. {
  43. type:'email',
  44. message: '邮箱格式不正确'
  45. }
  46. ]
  47. }
  48. ]"
  49. placeholder="请输入邮箱"
  50. />
  51. </div>
  52. </a-form-item>
  53. <a-form-item
  54. v-if="item.isreg *1 === 1"
  55. class="member-name"
  56. >
  57. <span class="has-register-btn">
  58. 已注册用户
  59. </span>
  60. </a-form-item>
  61. <a-form-item
  62. class="member-name"
  63. >
  64. <a-input
  65. v-decorator="[
  66. `truename[${index}]`,{
  67. rules:[{ required: true, message: '请输入姓名'}]
  68. }]"
  69. placeholder="姓名"
  70. />
  71. </a-form-item>
  72. <a-form-item>
  73. <a-button
  74. v-if="form.getFieldValue('keys').length > 1&&(index!==0)"
  75. class="del-invite-btn"
  76. @click="delteFormItem(index)"
  77. >
  78. 删除
  79. </a-button>
  80. </a-form-item>
  81. </div>
  82. <div class="invite-item">
  83. <a-form-item>
  84. <a-button
  85. class="add-invite-btn"
  86. @click="addItem"
  87. >
  88. 新增
  89. </a-button>
  90. </a-form-item>
  91. </div>
  92. <div class="invite-item">
  93. <a-form-item>
  94. <a-button
  95. type="primary"
  96. html-type="submit"
  97. :disabled="loading"
  98. >
  99. 发送邀请
  100. </a-button>
  101. </a-form-item>
  102. </div>
  103. </a-form>
  104. </div>
  105. </div>
  106. </div>
  107. </template>
  108. <script>
  109. import SecondCrumb from '@/components/pc/organization/invite/SecondCrumb'
  110. import { mapActions } from 'vuex'
  111. export default {
  112. components: {
  113. SecondCrumb
  114. },
  115. data () {
  116. return {
  117. initTeamId: '',
  118. isLoading: false,
  119. submitting: false,
  120. initItem: {},
  121. formData: [
  122. {
  123. email: '',
  124. truename: '',
  125. isreg: 0
  126. }
  127. ]
  128. }
  129. },
  130. computed: {
  131. loading () {
  132. return this.submitting
  133. },
  134. args () {
  135. return this.$route.query
  136. },
  137. keys () {
  138. let arr = []
  139. this.formData.forEach((val, index) => {
  140. arr.push(index)
  141. })
  142. return arr
  143. }
  144. },
  145. watch: {
  146. },
  147. created () {
  148. this.form = this.$form.createForm(this)
  149. this.form.getFieldDecorator('keys', { initialValue: [], preserve: true })
  150. this.form.getFieldDecorator('email', { initialValue: [], preserve: false })
  151. this.form.getFieldDecorator('truename', { initialValue: [], preserve: false })
  152. },
  153. mounted () {
  154. },
  155. methods: {
  156. ...mapActions('organization/invite', ['ajaxSave', 'ajaxEmail']),
  157. addItem () {
  158. this.formData.push(
  159. {
  160. email: '',
  161. truename: '',
  162. isreg: 0
  163. }
  164. )
  165. const { form } = this
  166. form.setFieldsValue({
  167. keys: this.keys
  168. })
  169. },
  170. // 删除邀请成员表单行
  171. delteFormItem (index) {
  172. const { form } = this
  173. // can use data-binding to set
  174. this.formData.splice(index, 1)
  175. form.getFieldValue('email').splice(index, 1)
  176. form.getFieldValue('truename').splice(index, 1)
  177. form.setFieldsValue({
  178. keys: this.keys,
  179. email: form.getFieldValue('email'),
  180. truename: form.getFieldValue('truename')
  181. })
  182. },
  183. setFormData (data) {
  184. const { formData } = this
  185. for (let i = 0; i < data.length; i++) {
  186. formData[i].truename = data[i]
  187. }
  188. },
  189. submitHandle () {
  190. this.form.validateFieldsAndScroll(['keys', 'email', 'truename'], async (err, values) => {
  191. const { form, formData } = this
  192. if (!err) {
  193. this.setFormData(form.getFieldValue('truename'))
  194. const chain = await this.ajaxSave()
  195. chain.params({ cid: this.args.cid * 1, inviteinfo: formData })
  196. .setForm(this.form)
  197. .fetch()
  198. }
  199. })
  200. },
  201. isEmailRe (rule, value, callback) {
  202. const length = this.form.getFieldValue('email').filter(item => item === value).length
  203. if (length > 1) {
  204. callback(new Error('邮箱重复!'))
  205. } else {
  206. callback()
  207. }
  208. },
  209. async isErrorEmail (rule, value, callback) {
  210. const index = this.form.getFieldValue('email').indexOf(value)
  211. const item = this.formData[index]
  212. let chain = await this.ajaxEmail()
  213. await chain.params({ email: value, cid: this.args.cid })
  214. .onNetSuccess((raw) => {
  215. const res = raw.data
  216. if (!res.error) {
  217. if (res.payload.isreg === 1) {
  218. // this.isHasReg = true
  219. item.isreg = 1
  220. item.truename = ''
  221. } else {
  222. item.isreg = 0
  223. }
  224. item.email = value
  225. } else {
  226. // item.msg = res.message
  227. // item.isreg = 3
  228. if (value && res.message) {
  229. callback(new Error(res.message))
  230. } else {
  231. callback()
  232. }
  233. }
  234. })
  235. .fetch()
  236. }
  237. }
  238. }
  239. </script>
  240. 复制代码

转载于:https://juejin.im/post/5cd941725188256805093bbd

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

闽ICP备14008679号