el-form 动态表单增减项 (vue+element ui)
element
A Vue.js 2.0 UI Toolkit for Web
项目地址:https://gitcode.com/gh_mirrors/eleme/element
免费下载资源
·
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、点击新增,弹出新增弹窗,添加表单项
// 新增选项
addItem(item, index) {
console.log("添加", item, index)
this.paramsVisible = true
},
// 取消参数弹窗
cancelParams() {
this.paramsForm.paramsName = '';
this.paramsVisible = false;
},
2、点击”-“,删除当前表单项
// 删除选项
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"
})
}
},
GitHub 加速计划 / eleme / element
54.06 K
14.63 K
下载
A Vue.js 2.0 UI Toolkit for Web
最近提交(Master分支:2 个月前 )
c345bb45
6 个月前
a07f3a59
* Update transition.md
* Update table.md
* Update transition.md
* Update table.md
* Update transition.md
* Update table.md
* Update table.md
* Update transition.md
* Update popover.md 6 个月前
更多推荐
已为社区贡献4条内容
所有评论(0)