组件库Ant Design Vue,这个组件看起来文档很详细,但也会遇到一些意想不到的问题,遇到的问题主要是当添加input的时候验证不能友好的使用,当一个input添加,然后删除,提交表单的时候会提示填写删除的input,还有个问题就是当把填写好的内容,删除提交的时候,已删除的内容还是能提交到后台,用方法处理
 
form.getFieldValue('email').splice(index, 1)

      form.getFieldValue('truename').splice(index, 1)

      form.setFieldsValue({

        keys: keys.filter(key => key !== index),

        email: form.getFieldValue('email'),

        truename: form.getFieldValue('truename')

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


 this.form.getFieldDecorator('keys', { initialValue: [0], preserve: true })

    this.form.getFieldDecorator('email', { initialValue: [], preserve: false })

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

  <div class="invite-new">

    <div class="invite-second-crumb">

      <second-crumb />

    </div>

    <div class="invite-box">

      <h2>通过邮件邀请,无需审核</h2>

      <p>发送邀请给指定的邮箱,对方只需进行个人账户设置即可直接加入团队</p>

      <div class="invite-list">

        <a-form

          ref="inviteForm"

          layout="inline"

          :form="form"

          class="demo-form-inline"

          @submit.prevent="submitHandle('inviteForm')"

        >

          <div

            v-for="(item,index) in formData"

            :key="index"

            class="invite-item"

          >

            <a-form-item

              :required="false"

            >

              <div class="input_email">

                <a-input

                  v-decorator="[

                    `email[${index}]`,

                    {

                      validateTrigger: [ 'blur'],

                      rules: [{

                                required: true,

                                whitespace: true,

                                message: '请输入邮箱'

                              },

                              {

                                validator: isEmailRe

                              },

                              {

                                validator:isErrorEmail

                              },

                              {

                                type:'email',

                                message: '邮箱格式不正确'

                              }

                      ]

                    }

                  ]"

                  placeholder="请输入邮箱"

                />

              </div>

            </a-form-item>

            <a-form-item

              v-if="item.isreg *1 === 1"

              class="member-name"

            >

              <span class="has-register-btn">

                已注册用户

              </span>

            </a-form-item>

            <a-form-item

              class="member-name"

            >

              <a-input

                v-decorator="[

                  `truename[${index}]`,{

                    rules:[{ required: true, message: '请输入姓名'}]

                  }]"

                placeholder="姓名"

              />

            </a-form-item>

            <a-form-item>

              <a-button

                v-if="form.getFieldValue('keys').length > 1&&(index!==0)"

                class="del-invite-btn"

                @click="delteFormItem(index)"

              >

                删除

              </a-button>

            </a-form-item>

          </div>

          <div class="invite-item">

            <a-form-item>

              <a-button

                class="add-invite-btn"

                @click="addItem"

              >

                新增

              </a-button>

            </a-form-item>

          </div>

          <div class="invite-item">

            <a-form-item>

              <a-button

                type="primary"

                html-type="submit"

                :disabled="loading"

              >

                发送邀请

              </a-button>

            </a-form-item>

          </div>

        </a-form>

      </div>

    </div>

  </div>

</template>

<script>

import SecondCrumb from '@/components/pc/organization/invite/SecondCrumb'

import { mapActions } from 'vuex'

export default {

  components: {

    SecondCrumb

  },

  data () {

    return {

      initTeamId: '',

      isLoading: false,

      submitting: false,

      initItem: {},

      formData: [

        {

          email: '',

          truename: '',

          isreg: 0

        }

      ]

    }

  },

  computed: {

    loading () {

      return this.submitting

    },

    args () {

      return this.$route.query

    },

    keys () {

      let arr = []

      this.formData.forEach((val, index) => {

        arr.push(index)

      })

      return arr

    }

  },

  watch: {

  },

  created () {

    this.form = this.$form.createForm(this)

    this.form.getFieldDecorator('keys', { initialValue: [], preserve: true })

    this.form.getFieldDecorator('email', { initialValue: [], preserve: false })

    this.form.getFieldDecorator('truename', { initialValue: [], preserve: false })

  },

  mounted () {

  },

  methods: {

    ...mapActions('organization/invite', ['ajaxSave', 'ajaxEmail']),

    addItem () {

      this.formData.push(

        {

          email: '',

          truename: '',

          isreg: 0

        }

      )

      const { form } = this

      form.setFieldsValue({

        keys: this.keys

      })

    },

    // 删除邀请成员表单行

    delteFormItem (index) {

      const { form } = this

      // can use data-binding to set

      this.formData.splice(index, 1)

      form.getFieldValue('email').splice(index, 1)

      form.getFieldValue('truename').splice(index, 1)

      form.setFieldsValue({

        keys: this.keys,

        email: form.getFieldValue('email'),

        truename: form.getFieldValue('truename')

      })

    },

    setFormData (data) {

      const { formData } = this

      for (let i = 0; i < data.length; i++) {

        formData[i].truename = data[i]

      }

    },

    submitHandle () {

   

      this.form.validateFieldsAndScroll(['keys', 'email', 'truename'], async (err, values) => {

        const { form, formData } = this

        if (!err) {

          this.setFormData(form.getFieldValue('truename'))

          const chain = await this.ajaxSave()

          chain.params({ cid: this.args.cid * 1, inviteinfo: formData })

            .setForm(this.form)

            .fetch()

        }

      })

    },

    isEmailRe (rule, value, callback) {

      const length = this.form.getFieldValue('email').filter(item => item === value).length

      if (length > 1) {

        callback(new Error('邮箱重复!'))

      } else {

        callback()

      }

    },

    async isErrorEmail (rule, value, callback) {

      const index = this.form.getFieldValue('email').indexOf(value)

      const item = this.formData[index]

      let chain = await this.ajaxEmail()

      await chain.params({ email: value, cid: this.args.cid })

        .onNetSuccess((raw) => {

          const res = raw.data

          if (!res.error) {

            if (res.payload.isreg === 1) {

              // this.isHasReg = true

              item.isreg = 1

              item.truename = ''

            } else {

              item.isreg = 0

            }

            item.email = value

          } else {

            // item.msg = res.message

            // item.isreg = 3

            if (value && res.message) {

              callback(new Error(res.message))

            } else {

              callback()

            }

          }

        })

        .fetch()

    }

  }

}

</script>
 
————————————————