使用element-ui +Vue 解决 table 里包含表单验证的问题
作者:buling girl 发布时间:2024-05-28 16:00:02
标签:element-ui,Vue,table,验证
应用场景:
在实际使用中经常会遇到需要在Form表单中使用table表格进行表单提交,同时又需要对table的字段进行校验,效果如图所示:
这个校验中,最关键的问题在于如何给el-form-item 动态绑定prop。
:prop="'tableData.' + scope.$index + '.字段名'"
方法一:
<template>
<div class="app-container">
<el-form :model="fromData" ref="from">
<el-table :data="fromData.domains">
<el-table-column label="姓名">
<template slot-scope="scope">
<el-form-item :prop="'domains.'+scope.$index+'.name'" :rules="fromaDataRules.name">
<el-input v-model="scope.row.name"></el-input>
</el-form-item>
</template>
</el-table-column>
<el-table-column label="地址">
<template slot-scope="scope">
<el-form-item :prop="'domains.'+scope.$index+'.desc'" :rules="fromaDataRules.desc">
<el-input v-model="scope.row.desc"></el-input>
</el-form-item>
</template>
</el-table-column>
</el-table>
</el-form>
<el-button type="warning" @click="submit('from')">submit</el-button>
</div>
</template>
<script>
export default {
data() {
return {
fromData:{
domains:undefined
},
fromaDataRules:{
name:[{ required: true, message: '请输入', trigger: 'blur' }],
desc:[ { required: true, message: '请填写', trigger: 'blur' }]
},
domains:[],
}
},
mounted(){
this.initDomains()
},
methods:{
initDomains(){
this.domains=[
{
name: "小红",
desc: "11123"
},
{
name: "小红",
desc: "11123"
}
]
},
init(){
this.$set(this.fromData,'domains',this.domains)
},
submit(formName){
this.$refs[formName].validate((valid) => {
if (valid) {
alert('submit!');
} else {
console.log('error submit!!');
return false;
}
});
}
}
}
</script>
上述代码中比较关键的部分有一下两点:
1、:prop="‘domains.'+scope.$index+'.name'" ,用于动态绑定prop到el-form-item;
2、this.$set(this.fromData,‘domains',this.domains) ,用于为fromData设置domains这个节点。
方法二:
<template>
<div class="app-container">
<el-form :model="fromData" ref="from">
<el-table :data="fromData.domains">
<el-table-column label="姓名">
<template slot-scope="scope">
<el-form-item :prop="'domains.'+scope.$index+'.name'" :rules="fromData.fromaDataRules.name">
<el-input v-model="scope.row.name"></el-input>
</el-form-item>
</template>
</el-table-column>
<el-table-column label="地址">
<template slot-scope="scope">
<el-form-item :prop="'domains.'+scope.$index+'.desc'" :rules="fromData.fromaDataRules.desc">
<el-input v-model="scope.row.desc"></el-input>
</el-form-item>
</template>
</el-table-column>
</el-table>
</el-form>
<el-button type="warning" @click="submit('from')">submit</el-button>
</div>
</template>
<script>
export default {
data() {
return {
fromData:{
fromaDataRules:{
name:[{ required: true, message: '请输入', trigger: 'blur' }],
desc:[ { required: true, message: '请填写', trigger: 'blur' }]
},
domains:[],
},
}
},
mounted(){
this.initDomains()
},
methods:{
initDomains(){
this.fromData.domains=[
{
name: "小红",
desc: "11123"
},
{
name: "小红",
desc: "11123"
}
]
},
init(){
},
submit(formName){
this.$refs[formName].validate((valid) => {
if (valid) {
alert('submit!');
} else {
console.log('error submit!!');
return false;
}
});
}
}
}
</script>
补充知识:Vue+ElementUI 完整增删查改验证功能的表格
我就废话不多说了,大家还是直接看代码吧~
<template>
<div class="block">
<el-col>
<el-row>
<el-form>
<el-form-item>
<el-input style="width: 250px;float: left" placeholder="请输入名称" v-model="query"></el-input>
<el-button @click="handleSelect" style="float: left;margin-left: 10px">查询</el-button>
<el-button @click="handleAdd" style="float: left;margin-left: 10px">新增</el-button>
</el-form-item>
</el-form>
</el-row>
<el-row>
<el-table
:data="tableData"
style="width: 100%"
border>
<el-table-column
prop="date"
label="日期"
width="250">
</el-table-column>
<el-table-column
prop="name"
label="姓名"
width="250">
</el-table-column>
<el-table-column
prop="address"
label="地址"
width="350">
</el-table-column>
<el-table-column>
<template slot-scope="scope">
<el-button size="mini" @click="handleEdit(scope.$index,scope.row)">编辑</el-button>
<el-button size="mini" type="danger" @click="handleDelete(scope.$index,scope.row)">删除</el-button>
</template>
</el-table-column>
</el-table>
</el-row>
<el-row>
<el-dialog class="dialog" :title="operation===true ?'新增':'编辑'" :visible.sync="dialogVisible" width="350px" >
<el-form label-width="80px" :model="lineData" :rules="addRule" ref="lineData" >
<el-form-item label="日期" prop="date">
<el-input v-model="lineData.date"></el-input>
</el-form-item>
<el-form-item label="姓名" prop="name">
<el-input v-model="lineData.name"></el-input>
</el-form-item>
<el-form-item label="地址">
<el-input v-model="lineData.address"></el-input>
</el-form-item>
<el-form-item>
<el-button @click="handleSave" type="primary">确定</el-button>
<el-button @click="handleCancel">取消</el-button>
</el-form-item>
</el-form>
</el-dialog>
</el-row>
</el-col>
</div>
</template>
<script>export default {
data () {
return {
operation: true,
dialogVisible: false,
lineData: {},
editData: {},
query: '',
addRule: {
date: [{required: true, message: '请输入日期', trigger: 'blur'}],
name: [{required: true, message: '请输入名称', trigger: 'blur'}]
},
tableData: [{
id: 1,
date: '2016-05-02',
name: '王一虎',
address: '上海市普陀区金沙江路 1518 弄'
}, {
id: 2,
date: '2016-05-04',
name: '王二虎',
address: '上海市普陀区金沙江路 1517 弄'
}, {
id: 3,
date: '2016-05-01',
name: '王一虎',
address: '上海市普陀区金沙江路 1519 弄'
}, {
id: 4,
date: '2016-05-03',
name: '王四虎',
address: '上海市普陀区金沙江路 1516 弄'
}]
}
},
methods: {
handleEdit (index, row) {
this.editData = JSON.stringify(row)
this.lineData = JSON.parse(this.editData)
this.dialogVisible = true
this.operation = false
},
handleDelete (index, row) {
this.tableData.splice(index, 1)
},
handleAdd () {
this.dialogVisible = true
this.operation = true
this.lineData = {}
this.lineData.id = Math.random()
},
handleSelect () {
if (this.query !== '') {
const tmpData = []
for (let item of this.tableData) {
if (item.name === this.query) {
tmpData.push(item)
}
}
this.tableData = tmpData
}
},
handleSave () {
this.$refs.lineData.validate((valid) => {
if (valid) {
this.addLine(this.lineData)
this.dialogVisible = false
} else {
alert('保存失败')
return false
}
})
},
handleCancel () {
this.dialogVisible = false
},
addLine (item) {
let existed = false
for (let i = 0; i < this.tableData.length; i++) {
if (this.tableData[i].id === item.id) {
this.tableData[i].id = item.id
this.tableData[i].name = item.name
this.tableData[i].address = item.address
this.tableData[i].date = item.date
existed = true
break
}
}
if (!existed) {
this.tableData.push(this.lineData)
}
}
}
}
</script>
<style scoped>
.block{
width: 75%;
margin-left: 400px;
margin-top: 200px;
}
</style>
来源:https://blog.csdn.net/u013746071/article/details/89705375


猜你喜欢
- 本文实例讲述了JS使用eval()动态创建变量的方法。分享给大家供大家参考,具体如下:一、什么是eval()函数?eval_r()函数可计算
- 1.第一个实例:HelloWorld1.编写python代码from flask import Flaskapp=Flask(__name_
- 为了今天要写的内容,运行了将近7个小时的程序,在数据库中存储了1千万条数据。——今天要说的是mysql数据库的IF()函数的一个实例。 具体
- 用面向对象的思维解决问题的重点当遇到一个需求的时候不用自己去实现,如果自己一步步实现那就是面向过程;应该找一个专门做这个事的人来做。面向对象
- 操作步骤进入命令行环境。我使用的是conda。有两种方式进入命令行。方法1:通过anconda navigator界面,选择environm
- 备注:Ken Henderson 从开发者的角度来阐述了SQL SERVER 2000内存管理的内部机制简介在本专栏中,我们将从一个开发者的
- httprouterhttprouter 是一个高性能、可扩展的HTTP路由,上面我们列举的net/http默认路由的不足,都被httpro
- 对一名开发者来说最糟糕的情况,莫过于要弄清楚一个不熟悉的应用为何不工作。有时候,你甚至不知道系统运行,是否跟原始设计一致。在线运行的应用就是
- 目录四种参数仅限关键字参数内省中的函数参数函数注解四种参数Python函数func定义如下:def func(first, *args, s
- 1.字符串函数 长度与分析用 datalength(Char_expr) 返回字符串包含字符数,但不包含后面的空格 substring(ex
- Python与星球大战背后的工业光魔提起Python语言,很多人会想起系统运维、Web开发等工作。很少有人会知道Python也能够用于电影视
- 一、使用ImageFolder读取数据集时忽略特定文件如果事先知道需要忽略哪些文件,当然直接从数据集里删除就行了。但如果需要在程序运行时动态
- 自定义路径转换器有时候上面的内置的url转换器并不能满足我们的需求,因此django给我们提供了一个接口可以让我们自己定义自己的url转换器
- set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go Create PROCEDURE [dbo].[
- BLOG地址:http://www.planabc.net/article.asp?id=107学习标准的朋友,一般都会在学习的过程中接触到
- 本文介绍了纯python进行矩阵的相乘运算的方法示例,分享给大家,具体如下:def matrixMultiply(A, B):
- 在WEB开发中.我们可能都习惯使用下面的代码来获取客户端的IP地址: C#代码 //优先取得 * string IP = Request
- 前言最近在工作中碰到一个小的排序问题,需要按嵌套对象的多个属性来排序,于是发现了Python里的operator模块和sorted函数组合可
- 什么是条形图?条形图(bar chart)是用宽度相同的条形的高度或长短来表示数据多少的图形。条形图可以横置或纵置,纵置时也称为柱形图(co
- 本文研究的主要是Python中optparser库的相关内容,具体如下。一直以来对optparser不是特别的理解,今天就狠下心,静下心研究