使用element-ui +Vue 解决 table 里包含表单验证的问题


Posted in Javascript onJuly 17, 2020

应用场景:

在实际使用中经常会遇到需要在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>

以上这篇使用element-ui +Vue 解决 table 里包含表单验证的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
什么是JavaScript
Aug 13 Javascript
在IE浏览器中resize事件执行多次的解决方法
Jul 12 Javascript
js里取容器大小、定位、距离等属性搜集整理
Aug 19 Javascript
javascript删除数组元素并且数组长度减小的简单实例
Feb 14 Javascript
上传图片预览JS脚本 Input file图片预览的实现示例
Oct 23 Javascript
jquery中map函数遍历数组用法实例
May 18 Javascript
实现高性能JavaScript之执行与加载
Jan 30 Javascript
vue双向绑定简要分析
Mar 23 Javascript
Vue.js对象转换实例
Jun 07 Javascript
详解Express笔记之动态渲染HTML(新手入坑)
Dec 13 Javascript
一个手写的vue放大镜效果
Aug 09 Javascript
vue 动态添加class,三个以上的条件做判断方式
Nov 02 Javascript
Vue element-ui父组件控制子组件的表单校验操作
Jul 17 #Javascript
简单了解常用的JavaScript 库
Jul 16 #Javascript
jQuery加PHP实现图片上传并提交的示例代码
Jul 16 #jQuery
浅谈js中的attributes和Attribute的用法与区别
Jul 16 #Javascript
JS自定义右键菜单实现代码解析
Jul 16 #Javascript
JS如何在不同平台实现多语言方式
Jul 16 #Javascript
vue使用axios实现excel文件下载的功能
Jul 16 #Javascript
You might like
德生S2000收音机更换“钕铁硼”全频扬声器
2021/03/02 无线电
详解php的魔术方法__get()和__set()使用介绍
2012/09/19 PHP
深入理解require与require_once与include以及include_once的区别
2013/06/05 PHP
PHP实现获取域名的方法小结
2014/11/05 PHP
jQuery+php简单实现全选删除的方法
2016/11/28 PHP
PHP pthreads v3在centos7平台下的安装与配置操作方法
2020/02/21 PHP
jQuery EasyUI API 中文文档 - Documentation 文档
2011/09/29 Javascript
javascript判断两个IP地址是否在同一个网段的实现思路
2013/12/13 Javascript
js opener的使用详解
2014/01/11 Javascript
javascript替换已有元素replaceChild()使用介绍
2014/04/03 Javascript
JS获取当前网页大小以及屏幕分辨率等
2014/09/05 Javascript
两种方法基于jQuery实现IE浏览器兼容placeholder效果
2014/10/14 Javascript
全面解析Bootstrap中tab(选项卡)的使用方法
2016/06/06 Javascript
利用jQuery对无序列表排序的简单方法
2016/10/16 Javascript
详解javascript中对数据格式化的思考
2017/01/23 Javascript
js 取消页面可以选中文字的功能方法
2018/01/02 Javascript
Python中函数的参数定义和可变参数用法实例分析
2015/06/04 Python
Python字符编码判断方法分析
2016/07/01 Python
Python环境Pillow( PIL )图像处理工具使用解析
2019/09/12 Python
Python使用matplotlib 画矩形的三种方式分析
2019/10/31 Python
python随机数分布random均匀分布实例
2019/11/27 Python
python基于plotly实现画饼状图代码实例
2019/12/16 Python
浅谈matplotlib.pyplot与axes的关系
2020/03/06 Python
Python PyQt5运行程序把输出信息展示到GUI图形界面上
2020/04/27 Python
Python判断变量是否是None写法代码实例
2020/10/09 Python
【魔兽争霸3重制版】原版画面与淬火MOD画面对比
2021/03/26 魔兽争霸
物流毕业生个人的自我评价
2014/02/13 职场文书
经典而简洁的婚礼主持词
2014/03/13 职场文书
建筑院校毕业生求职信
2014/06/13 职场文书
建筑工程技术专业求职信
2014/07/16 职场文书
中韩经贸翻译专业大学生职业生涯规划范文
2014/09/18 职场文书
2014年党支部书记工作总结
2014/12/04 职场文书
2015年财务人员工作总结
2015/04/10 职场文书
2016学习雷锋精神活动倡议书
2015/04/27 职场文书
人口与计划生育责任书
2015/05/09 职场文书
浅谈Redis 中的过期删除策略和内存淘汰机制
2022/04/03 Redis