最近作項目用的組件庫Ant Design Vue,這個組件看起來文檔很詳細,但也會遇到一些意想不到的問題,最後看文檔沒辦法解決,只能去看源碼,折騰了很久,終於把問題給解決了,遇到的問題主要是當添加input的時候驗證不能友好的使用,當一個input添加,而後刪除,提交表單的時候會提示填寫刪除的input,還有個問題就是當把填寫好的內容,刪除提交的時候,已刪除的內容仍是能提交到後臺,用方法處理html
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方法才能成功,可是添加了以後也沒刪除成功,vue
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數組中不會出現空元素現象,在表單提交的時候加上須要驗證的字段,這樣問題就解決了,大體的代碼以下ajax
<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>
複製代碼