1、点击”+“,弹出弹窗,新增一项,点击”-“,删除当前项
代码展示:
html代码:
注意:
el-form-item(表单项)循环,绑定的数组写在form当中
表单:
<el-form :model="form">
<el-form-item label="设备类型:" :label-width="formLabelWidth">
<el-select v-model="form.region" placeholder="请选择活动区域">
<el-option label="区域一" value="shanghai"></el-option>
<el-option label="区域二" value="beijing"></el-option>
</el-select>
</el-form-item>
<el-form-item v-for="(item, index) in form.formItemData" :key="item.id" :label="item.label"
:label-width="formLabelWidth">
<div class="formItem">
<el-input v-model="item.value" autocomplete="off"></el-input>
<el-button v-if="form.formItemData.length - 1 == index" style="margin-left: 20px;"
@click="addItem(item, index)">+</el-button>
<el-button style="margin-left: 20px;" @click="delItem(item, index)">-</el-button>
</div>
</el-form-item>
</el-form>
新增参数弹框:
<!-- 新增参数弹窗 -->
<el-dialog :close-on-click-modal="false" title="新增参数" :visible.sync="paramsVisible">
<el-form :model="paramsForm">
<el-form-item label="参数名称" label-width="120px">
<el-input v-model="paramsForm.paramsName" autocomplete="off"></el-input>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="cancelParams">取 消</el-button>
<el-button type="primary" @click="addParams">确 定</el-button>
</div>
</el-dialog>
data:
//表单数据
form: {
region: '',
formItemData: [],
},
//新增弹窗数据
paramsVisible: false,
paramsForm: {
paramsName: "",
}
methods:
1、点击新增,弹出新增弹窗,添加表单项文章来源:https://www.toymoban.com/news/detail-786585.html
// 新增选项
addItem(item, index) {
console.log("添加", item, index)
this.paramsVisible = true
},
// 取消参数弹窗
cancelParams() {
this.paramsForm.paramsName = '';
this.paramsVisible = false;
},
2、点击”-“,删除当前表单项文章来源地址https://www.toymoban.com/news/detail-786585.html
// 删除选项
delItem(item, index) {
//item:当前表单项 index:当前项的下标
console.log("删除", item, index)
if (this.form.formItemData.length > 1) {
this.form.formItemData.splice(index, 1)
} else {
this.$message({
message: "最少保留一项",
type: "warning"
})
}
},
到了这里,关于el-form 动态表单增减项 (vue+element ui)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!