总结/朱季谦

最近通过Vue + Element ui实现了动态表单功能,该功能还包括了动态表单新增行、删除行、动态表单验证、动态表单提交功能,趁热打铁,将开发心得记录下来,方便以后再遇到类似功能时,直接拿来应用。

简化的页面效果图如下:

最开始,我是用了纯粹的表格形式,后来发现,这种形式在提交的时候,不好对每个输入框做校验,若是表单形式话,就可以直接通过rule设置每个输入框的验证,因此,我就在表格里面嵌套了表单。注意一点是,el-form-item里的 :prop="scope.$index + '.name'"需要对应el-input的 v-model="studentData[scope.$index].name",两者都是同一个字段值。

<template>
<div >
<div>
<div>
<el-button size="small" @click="addRow">新增</el-button>
</div>
<!--设置的表单-->
<el-form :model="studentData" ref="data" label-width="auto">
<el-table
border
:header-cell-style="{ 'text-align': 'center' }"
:cell-style="{ 'text-align': 'center' }"
:data="studentData"
ref="table"
style="width: 100%"
> <el-table-column align="center" label="姓名">
<template slot-scope="scope">
<!--表格里面嵌套表单-->
<el-form-item
:prop="scope.$index + '.name'"
:rules="{ required: true, message: '姓名不能为空', trigger: 'blur' }"
>
<el-input
v-model="studentData[scope.$index].name"
autocomplete="off"
size="small"
placeholder="姓名"
></el-input>
</el-form-item>
</template>
</el-table-column> <el-table-column align="center" label="年龄">
<template slot-scope="scope">
<el-form-item
:prop="scope.$index + '.age'"
:rules="{ required: true, message: '年龄不能为空', trigger: 'blur' }"
>
<el-input
v-model="studentData[scope.$index].age"
autocomplete="off"
size="small"
type='number'
placeholder="收款方开户行号"
></el-input>
</el-form-item>
</template>
</el-table-column> <el-table-column align="center" label="性别">
<template slot-scope="scope">
<el-form-item
:prop="scope.$index + '.sex'"
:rules="{ required: true, message: '性别不能为空', trigger: 'blur' }"
>
<el-input
v-model="studentData[scope.$index].sex"
autocomplete="off"
size="small"
placeholder="性别"
></el-input>
</el-form-item>
</template>
</el-table-column> <el-table-column fixed="right" label="操作" width="100">
<template slot-scope="scope">
<el-button
@click="handleDeleteRow(studentData[scope.$index])"
type="text"
size="small"
>删除</el-button
>
</template>
</el-table-column> </el-table>
</el-form>
</div> <div slot="footer" class="dialog-footer" style="margin-bottom: 10px">
<el-button size="mini" @click="submit">提交</el-button>
<el-button size="mini" @click="resetForm()">重置</el-button>
</div>
</div>
</template>

定义一个存储动态表格数据的数组变量

export default {
data() {
return {
studentData:[],
};
},
......
}

在methods方法里增加相关方法,分别是新增行、删除行、提交——

methods:{

  /**
* 新增行
*/
addRow() {
let index = this.studentData.length ;
this.studentData.push({
key: index,
name:'',
age:'',
sex:'',
});
}, /**
* 删除行
* @param row
*/
handleDeleteRow(row){
let datas = this.studentData;
for (var i = 0; i < datas.length; i++){
if (datas[i].key == row.key){
datas.splice(i,1);
}
}
}, /**
* 提交
*/
submit() {
this.$refs["data"].validate(valid => {
//valid为true,表示表单都已经验证通过,若为false,说明存在表单验证失败
if (valid) {
save(this.studentData).then(response => {
this.$message({
message: '提交成功',
type: 'success'
});
});
}
});
}, /**
* 重置
*/
resetForm() {
let datas = this.studentData;
for (var i = 0; i < datas.length; i++){
datas[i].name='';
datas[i].age='';
datas[i].sex='';
}
},
}

设置表单验证规则,可统一在rules设置,也可以在每一输入框单独设置,我这里是单独在每一个输入框里设置,即:rules="{ required: true, message: '姓名不能为空', trigger: 'blur' }"就可以了,当然,还可以做一些更复杂的自定义规则。

<el-table-column align="center"   label="姓名">
<template slot-scope="scope">
<!--表格里面嵌套表单-->
<el-form-item
:prop="scope.$index + '.name'"
:rules="{ required: true, message: '姓名不能为空', trigger: 'blur' }"
>
<el-input
v-model="studentData[scope.$index].name"
autocomplete="off"
size="small"
placeholder="姓名"
></el-input>
</el-form-item>
</template>
</el-table-column>

Vue + Element ui 实现动态表单,包括新增行/删除行/动态表单验证/提交功能的更多相关文章

  1. vue + element ui 阻止表单输入框回车刷新页面

    问题 在 vue+element ui 中只有一个输入框(el-input)的情况下,回车会提交表单. 解决方案 在 el-form 上加上 @submit.native.prevent 这个则会阻止 ...

  2. vue + element ui 实现实现动态渲染表格

    前言:之前需要做一个页面,能够通过表名动态渲染出不同的表格,这里记录一下.转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9786326.html 网站地址:我的 ...

  3. vue+element ui 的tab 动态增减,切换时提示用户是否切换

    前言:工作中用到 vue+element ui 的前端框架,动态添加 Tab,删除 Tab,切换 Tab 时提示用户是否切换等,发现 element ui  有一个 bug,这里记录一下如何实现.转载 ...

  4. 基于vue(element ui) + ssm + shiro 的权限框架

    zhcc 基于vue(element ui) + ssm + shiro 的权限框架 引言 心声 现在的Java世界,各种资源很丰富,不得不说,从分布式,服务化,orm,再到前端控制,权限等等玲琅满目 ...

  5. 基于 vue+element ui 的cdn网站(多页面,都是各种demo)

    前言:这个网站持续更新中...,有网上预览,github上也有源码,喜欢记得star哦,欢迎留言讨论. 网站地址:我的个人vue+element ui demo网站 github地址:yuleGH g ...

  6. vue+element ui中select组件选择失效问题原因与解决方法

    codejing 2020-07-10 09:13:31  652  收藏 分类专栏: Web Vue Element UI 版权 .当表单form赋完值后,如果后续又对form中某一属性值进行操作如 ...

  7. 分享一个自搭的框架,使用Spring boot+Vue+Element UI

    废弃,新的:https://www.cnblogs.com/hackyo/p/10453243.html 特点:前后端分离,可遵循restful 框架:后端使用Spring boot,整合了aop.a ...

  8. Vue + Element UI 实现权限管理系统

    Vue + Element UI 实现权限管理系统 前端篇(一):搭建开发环境 https://www.cnblogs.com/xifengxiaoma/p/9533018.html

  9. vue + element ui 表格自定义表头,提供线上demo

    前言:工作中用到 vue+element ui 的前端框架,需要使用自定义表头,需要使用 re.转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9710826.h ...

  10. vue+element ui 的上传文件使用组件

    前言:工作中用到 vue+element ui 的前端框架,使用到上传文件,则想着封装为组件,达到复用,可扩展.转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9 ...

随机推荐

  1. 【从零开始】Docker Desktop:听说你小子要玩我

    前言 缘由 捡起遗忘的Docker知识 由于本狗近期项目紧任务重,高强度的搬砖导致摸鱼时间下降.在上线项目时,看到运维大神一系列骚操作,docker+k8s的知识如过眼云烟,忘得干净的很.所以想重新恶 ...

  2. 狠狠地切割(Hard Version)

    狠狠地切割(Hard Version) (https://www.luogu.com.cn/problem/P8889) 跟easy版非常像,但是数据太大开标记数组的话会爆所.以得转换一下 开一个ma ...

  3. [Pytorch框架] 1.7 数据并行

    数据并行(选读) Authors: Sung Kim and Jenny Kang 在这个教程里,我们将学习如何使用 DataParallel 来使用多GPU. PyTorch非常容易就可以使用多GP ...

  4. 文章学习:TPRE:分布式门限代理重加密

    学习文章:TPRE:分布式门限代理重加密 前言 成方金科新技术实验室与隐语团队合作,构建了"基于国密的分布式门限代理重加密算法TPRE",为用户提供了一种安全.高效.自主可控的数据 ...

  5. 使用ChatGPT4协助完成读取文件中不同字的数量

    使用ChatGPT4识别:用java读取文件中不同字的个数. 解析:该程序将读取名为"file.txt"的文件,并计算文件中每个不同字的出现次数.它使用一些字符串操作来清理单词,并 ...

  6. 【Visual C#】基于《斗鱼弹幕服务器第三方接入协议v1.6.2》实现斗鱼弹幕服务器接入

    最近在给某个主播开发斗鱼直播间辅助工具,为了程序的高效稳定,也搜索了大量的资料,经过大量什么百度,谷歌搜索... 虽然有很多Python的脚本及JS脚本实现了拉取斗鱼弹幕信息,但是这些年来的开发职业病 ...

  7. 2022-09-23:整数数组 stations 表示 水平数轴 上各个加油站的位置。给你一个整数 k 。 请你在数轴上增设 k 个加油站, 新增加油站可以位于 水平数轴 上的任意位置,而不必放在整数

    2022-09-23:整数数组 stations 表示 水平数轴 上各个加油站的位置.给你一个整数 k . 请你在数轴上增设 k 个加油站, 新增加油站可以位于 水平数轴 上的任意位置,而不必放在整数 ...

  8. 2020-11-01:rust中带move闭包和不带move闭包有什么区别?

    福哥答案2020-11-01: 1.是否是同一个变量:带move闭包,函数外和函数内的同名变量不是同一个变量.不带move闭包,函数外和函数内的同名变量是同一个变量.2.执行完闭包后:带move闭包, ...

  9. 2020-12-07:go中,slice的底层数据结构是什么?

    福哥答案2020-12-07: 源码位于runtime/slice.go文件中的slice结构体. type slice struct { array unsafe.Pointer len int c ...

  10. ET框架6.0分析三、网络通信

    概述 ET框架的消息机制贯彻始终,包含Entity消息(Awake,Update ...),自定义(Customer)消息,网络消息等.而ET系统的进程包含了客户端.Gate等各种类型的服务器,进程包 ...