记录一下对这个组件的使用。
源代码是这样的:
<el-form :model="dynamicValidateForm" ref="dynamicValidateForm" label-width="100px" class="demo-dynamic">
<el-form-item
prop="email"
label="邮箱"
:rules="[
{ required: true, message: '请输入邮箱地址', trigger: 'blur' },
{ type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
]"
>
<el-input v-model="dynamicValidateForm.email"></el-input>
</el-form-item>
<el-form-item
v-for="(domain, index) in dynamicValidateForm.domains"
:label="'域名' + index"
:key="domain.key"
:prop="'domains.' + index + '.value'"
:rules="{
required: true, message: '域名不能为空', trigger: 'blur'
}"
>
<el-input v-model="domain.value"></el-input><el-button @click.prevent="removeDomain(domain)">删除</el-button>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm('dynamicValidateForm')">提交</el-button>
<el-button @click="addDomain">新增域名</el-button>
<el-button @click="resetForm('dynamicValidateForm')">重置</el-button>
</el-form-item>
</el-form>
<script>
export default {
data() {
return {
dynamicValidateForm: {
domains: [{
value: ''
}],
email: ''
}
};
},
methods: {
submitForm(formName) {
this.$refs[formName].validate((valid) => {
if (valid) {
alert('submit!');
} else {
console.log('error submit!!');
return false;
}
});
},
resetForm(formName) {
this.$refs[formName].resetFields();
},
removeDomain(item) {
var index = this.dynamicValidateForm.domains.indexOf(item)
if (index !== -1) {
this.dynamicValidateForm.domains.splice(index, 1)
}
},
addDomain() {
this.dynamicValidateForm.domains.push({
value: '',
key: Date.now()
});
}
}
}
</script>
但是我的需求是:自动增减的表单项是下拉选,从所有人中选择审批人,自动增减的就是审批人1,审批人2...
面对源代码的增减项是一个input输入框,我是这样改的:
<el-form-item
v-for="(user, index) in model.userList"
:label="'审批人' + (index + 1)"
:rules="{required: true, message: '审批人不能为空', trigger: 'blur'}">
<template slot-scope="scope">
<el-select
v-model="model.userList[index]"
value-key="userId" placeholder="请选择">
<el-option
v-for="item in teacherList"
:key="item.userId"
:label="item.userName"
:value="item">
</el-option>
</el-select>
<el-button @click.prevent="removeDomain(user)">删除</el-button>
</template>
</el-form-item>
<el-form-item>
<el-button @click="addDomain">添加审批人</el-button>
<el-button type="primary" @click="goBack">返回</el-button>
<el-button type="primary" @click="saveModel">保存</el-button>
</el-form-item>
</el-form>
还有一点值得记录得就是下拉选,如果想保存的是整个对象,就得用上value-key。
下拉选的回显,之前的属性和下拉选List里的对象的属性是一样的才能显示出来,不然是不能回显的。
好了,还有好多工作待完成,这篇写的着实潦草了些,不过因为昨天我用到这个功能的时候以为我早就记录过了,结果并没有,所以非常有必要先记录下来。以后有时间在完善吧。