当前位置:网站首页>动态表单配置select
动态表单配置select
2022-08-11 05:17:00 【-加油】
1、通过el-form-item v-for=“(formName, index) in dynamicForm.formNames”
遍历多个动态表单,
<el-form-item
v-for="(formName, index) in dynamicForm.formNames"
:key="formName.key"
:lable="'域名' + (index + 1)"
:prop="'formNames.' + index + '.value'"
:rules="{
required: true,
message: '不能为空,请选择',
trigger: 'blur',
}"
>
<el-select v-model="formName.value" placeholder="请选择">
<el-option
v-for="item in filterOptions[index]"
:key="item.value"
:label="item.label"
:value="item.value"
>
</el-option>
</el-select>
<el-button @click.prevent="removeForm(formName)">删除表单</el-button>
</el-form-item>
数据项:
// 动态表单
dynamicForm: {
formNames: [
{
value: ''
}
],
email: ''
},
默认的下拉框属性值:
// 原始options是不能更改的
options: [
{
value: 1,
label: '臭豆腐'
},
{
value: 2,
label: '榴莲'
},
{
value: 3,
label: '炸鸡'
},
{
value: 4,
label: '蛋挞'
},
{
value: 5,
label: '披萨'
},
]
遍历的option是用计算属性
el-option v-for=“item in filterOptions[index]”
computed: {
// filterOptions所依赖的数据是formNames,添加表单时会更改,当选择下拉值时value更改也会重新计算
filterOptions () {
// itemOptions : [ [options1],[options2],[options3],[options4],[options5]]
const itemOptions = this.dynamicForm.formNames.map((item, index) => {
// checkIds已经被选中的。比如第一个表单选中了臭豆腐,value就是1,遍历到第二个表单时就要考虑第一个表单已经选中的。
// 当item是第二个表单时,再遍历formNames,val再从新开始,因为第一个被选了,所以val.value 不为空,且被选的val.value不等于当前item的value
// 也就是自己的item.value要留下来,filter进行选择的已选中不包含自己
const checkIds = this.dynamicForm.formNames.filter(val => val.value !== '' && val.value !== item.value).map(v => v.value)
console.log('checkIds是', checkIds)
// list 就是当前item可以进行选择的 把已经选择的去掉
const list = this.options.filter(val => !checkIds.includes(val.value))
return list
})
return itemOptions
}
}
添加表单:
<el-form-item>
<el-button type="primary" @click="submitForm(dynamicForm)"
>提交</el-button
>
<el-button @click="addForm">新增表单</el-button>
</el-form-item>
实现添加表单
addForm () {
if (this.dynamicForm.formNames.length === 5) {
this.$message({
message: '最多添加5个表单',
type: 'warning'
})
return
}
this.dynamicForm.formNames.push({
value: '',
key: Date.now()
})
}
实现删除表单:
removeForm (item) {
if (this.dynamicForm.formNames.length === 1) {
this.$message({
message: '最少1个表单',
type: 'warning'
})
return
}
var index = this.dynamicForm.formNames.indexOf(item)
if (index !== -1) {
this.dynamicForm.formNames.splice(index, 1)
}
},
随意的提交
submitForm (formNames) {
this.$refs.dynamicFormRef.validate(valid => {
if (valid) {
alert('提交啦')
} else {
return false
}
})
},
全部代码:
<template>
<div id="app">
<div class="box">
<el-form :model="dynamicForm" ref="dynamicFormRef" label-width="80px">
<el-form-item
v-for="(formName, index) in dynamicForm.formNames"
:key="formName.key"
:lable="'域名' + (index + 1)"
:prop="'formNames.' + index + '.value'"
:rules="{
required: true,
message: '不能为空,请选择',
trigger: 'blur',
}"
>
<el-select v-model="formName.value" placeholder="请选择">
<el-option
v-for="item in filterOptions[index]"
:key="item.value"
:label="item.label"
:value="item.value"
>
</el-option>
</el-select>
<el-button @click.prevent="removeForm(formName)">删除表单</el-button>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm(dynamicForm)"
>提交</el-button
>
<el-button @click="addForm">新增表单</el-button>
</el-form-item>
</el-form>
</div>
</div>
</template>
<script>
export default {
data () {
return {
// 动态表单
dynamicForm: {
formNames: [
{
value: ''
}
],
email: ''
},
// 原始options是不能更改的
options: [
{
value: 1,
label: '臭豆腐'
},
{
value: 2,
label: '榴莲'
},
{
value: 3,
label: '炸鸡'
},
{
value: 4,
label: '蛋挞'
},
{
value: 5,
label: '披萨'
},
]
}
},
methods: {
submitForm (formNames) {
this.$refs.dynamicFormRef.validate(valid => {
if (valid) {
alert('提交啦')
} else {
return false
}
})
},
removeForm (item) {
if (this.dynamicForm.formNames.length === 1) {
this.$message({
message: '最少1个表单',
type: 'warning'
})
return
}
var index = this.dynamicForm.formNames.indexOf(item)
if (index !== -1) {
this.dynamicForm.formNames.splice(index, 1)
}
},
addForm () {
if (this.dynamicForm.formNames.length === 5) {
this.$message({
message: '最多添加5个表单',
type: 'warning'
})
return
}
this.dynamicForm.formNames.push({
value: '',
key: Date.now()
})
}
},
computed: {
// filterOptions所依赖的数据是formNames,添加表单时会更改,当选择下拉值时value更改也会重新计算
filterOptions () {
// itemOptions : [ [options1],[options2],[options3],[options4],[options5]]
const itemOptions = this.dynamicForm.formNames.map((item, index) => {
// checkIds已经被选中的。比如第一个表单选中了臭豆腐,value就是1,遍历到第二个表单时就要考虑第一个表单已经选中的。
// 当item是第二个表单时,再遍历formNames,val再从新开始,因为第一个被选了,所以val.value 不为空,且被选的val.value不等于当前item的value
// 也就是自己的item.value要留下来,filter进行选择的已选中不包含自己
const checkIds = this.dynamicForm.formNames.filter(val => val.value !== '' && val.value !== item.value).map(v => v.value)
console.log('checkIds是', checkIds)
// list 就是当前item可以进行选择的 把已经选择的去掉
const list = this.options.filter(val => !checkIds.includes(val.value))
return list
})
return itemOptions
}
}
}
</script>
<style>
#app {
font-family: Helvetica, sans-serif;
text-align: center;
}
.box {
width: 800px;
height: auto;
margin: 0 auto;
}
</style>
边栏推荐
- (3) Construction of a real-time performance monitoring platform (Grafana+Prometheus+Node_explorer+Jmeter)
- C语言结构体——位段概念的讲解
- Django--20 implements Redis support, context, and interaction of context and interface
- 简单做份西红柿炒蛋778
- 开炮,开炮
- Blender 初教程
- Minecraft
- 全国青少年信息学奥林匹克联赛大纲
- Install different versions of MinGW (g++/gcc) and the configuration of the corresponding clion editor under Win
- 【win10+cuda7.5+cudnn6.0安装caffe⑥】报错及处理方式
猜你喜欢

【网站小白】mySQL数据库异常断开

C语言动态内存分配(1)三种函数讲解

09-ES6语法:变量、箭头函数、类语法、静态属性及非静态属性

pytorch安装笔记——Pytorch在conda+CUDA10.2环境安装task01

04-开发自己的npm包及发布流程详细讲解

吃瓜教程task05 第6章 支持向量机

uniapp获取用户信息(登录及个人中心页面的实现)

C语言——文件操作详解(1)

【CSDN21天学习挑战赛】第一天,配置环境外加实现mnist手写数字识别

npm install 时报 npm ERR Cannot read properties of null (reading ‘pickAlgorithm‘)
随机推荐
05-Nodejs中的模块加载机制
吃瓜教程task02 第3章 线性模型
【网站小白】mySQL数据库异常断开
开炮,开炮
【网站小白】Hibernate插入数据成功,不报错,但是数据库中没有值
博客帮助文档
Markdown 常用到的一些编写技巧
Visual Studio上一些Error的解决方案
吃瓜教程task03 第4章 决策树
06-引入Express创建web服务器、接口封装并使用postman测试,静态资源托管
C语言结构体详解 (2) 结构体内存对齐,默认对齐数
程序员小白的自我救赎之路。
【翻译】博客游戏项目Q1K3 – 制作
【win10+cuda7.5+cudnn6.0安装caffe①】安装cuda和cudnn
吃瓜教程task04 第5章 神经网络
【分享】一个免费语料库
05-JS中的BOM和DOM
第4章 复合类型-2(指针)
ClionIDE compiles by specifying the compiler
leetcode21.合并两个有序链表