<template>
<div>
<el-form :model="formData" ref="formData" hide-required-asterisk style="width: 900px;" size="small">
<el-row class="el-row">
<el-radio-group v-model="formData.radio1">
<el-radio-button label="1">选项1</el-radio-button>
<el-radio-button label="2">选项2</el-radio-button>
</el-radio-group>
</el-row>
<el-row v-for="(section, index) in formData.sections" :key="index">
<el-col :span="6">
<el-form-item label="满" label-width="20px" :prop="'sections.' + index + '.oneId'" :rules="rules.required">
<el-input v-model="section.oneId" placeholder="请输入内容" style="width: 200px;margin-right: 10px;">
<template slot="append">元</template>
</el-input>
</el-form-item>
</el-col>
<el-col :span="6">
<el-form-item :label="formData.radio1 === '1' ? '立减' : '打'" label-width="40px"
:prop="'sections.' + index + '.twoId'" :rules="rules.required">
<el-input v-model="section.twoId" placeholder="请输入内容" style="width: 180px;margin-right: 10px;">
<template slot="append">{{ formData.radio1 === "1" ? "元" : "折" }}</template>
</el-input>
</el-form-item>
</el-col>
<el-col :span="6" v-if="formData.radio1 === '2'">
<el-form-item label="最高折扣价格" label-width="100px" :prop="'sections.' + index + '.threeId'"
:rules="rules.required">
<el-input v-model="section.threeId" placeholder="请输入内容" style="width: 130px;margin-right: 10px;"></el-input>
</el-form-item>
</el-col>
<el-col :span="6" v-if="index === 0">
<el-form-item label="">
<el-button @click="newSection()" type="primary" style="margin-left: 10px;">新增标段</el-button>
</el-form-item>
</el-col>
<el-col :span="6" v-if="index !== 0">
<el-form-item label="">
<el-button @click="delSection(index)" style="margin-left: 10px;">删除</el-button>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col>
<el-form-item>
<el-button @click="handleSubmin">提交</el-button>
<el-button @click="resetForm">重置</el-button>
</el-form-item>
</el-col>
</el-row>
</el-form>
</div>
</template>
<script>
export default {
data() {
return {
formData: {
radio1: "1",
sections: [{ oneId: '', twoId: '', threeId: '' }]
},
rules: {
required: [
{ required: true, message: '此项不能为空' },
{
required: true, validator: (rule, value, callback) => {
if (value === null || value === undefined || value === '' || isNaN(value) || value <= 0) {
callback(new Error("请输入合法值"));
}
callback();
}
},
]
},
}
},
methods: {
/**
新增标段
*/
newSection() {
const { sections } = this.formData;
let flag, validateFieldArrs = [];
sections.forEach((sec, index) => {
//部分验证validateField接收的参数格式:['list.0.id', 'list.1.id']
validateFieldArrs.push(`sections.${index}.oneId`, `sections.${index}.twoId`, `sections.${index}.threeId`);
if (this.formData.radio1 === "1") {
if (sec.oneId && sec.twoId) {
flag = true;
} else {
flag = false;
}
} else {
if (sec.oneId && sec.twoId && sec.threeId) {
flag = true;
} else {
flag = false;
}
}
})
if (flag) {
//全部验证通过执行新增
this.formData.sections.push({ oneId: '', twoId: '', threeId: '' });
} else {
//验证指定表单
this.$refs['formData'].validateField(validateFieldArrs);
}
},
/**
删除标段
@params {Number} index 删除数据索引
*/
delSection(index) {
this.formData.sections.splice(index, 1)
},
/**
* 提交
*/
handleSubmin() {
this.$refs['formData'].validate((valid) => {
if (valid) {
alert('submit!');
}
});
},
/**
* 重置
*/
resetForm() {
this.$refs['formData'].resetFields();
},
},
}
</script>
<style lang='scss' scoped>
::v-deep .el-row {
margin-bottom: 10px;
}
</style>
vue2 + elementUI 动态添加表单项,并验证
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 项目中经常会用到动态添加表单项的功能 一般遇到这个功能,我们必须要考虑到一些问题: 验证前面是否有空项,有空则不能...
- Element中表单验证的基本方法可参照 官方说明 这是官方的一个例子,例子中包含一个表单,数据结构是: "域名"...
- ``` <el-form-item v-if="0===index" :label="$t('模板内容值')" ...