vue element table中自定义一些input的验证操作


Posted in Javascript onJuly 18, 2020

官网原话

Form 组件提供了表单验证的功能,只需要通过 rules 属性传入约定的验证规则,并将 Form-Item 的 prop 属性设置为需校验的字段名即可。

表单

el-form表单必备以下三个属性:

:model="ruleForm" 绑定的数据内容

:rules="rules" 动态绑定的rules,表单验证规则

ref="ruleForm" 绑定的对象

template模块

其实问题关键就在于如何给el-form-item动态绑定prop

:prop="'tableData.' + scope.$index + '.字段名'"

<template>
 <div class="TestWorld">
  <el-button @click="addLine">添加行数</el-button>
  <el-button @click="save('formDom')">baocun</el-button>
  <el-form :rules="formData.rules" :model="formData" ref="formDom" class="demo-ruleForm">
  <el-table
   :data="formData.tableData"
   style="width: 100%">
   <el-table-column prop="bookname" label="书名">
    <template slot-scope="scope">
    <el-form-item :prop="'tableData.' + scope.$index + '.bookname'" :rules='formData.rules.name'>
     <el-input v-model="scope.row.bookname" placeholder="书名" ></el-input>
    </el-form-item>
    </template>
   </el-table-column>
   <el-table-column prop="bookvolume" label="册数">
    <template slot-scope="scope">
     <el-form-item :prop="'tableData.' + scope.$index + '.bookvolume'" :rules="formData.rules.volume1">
     <el-input v-model.number="scope.row.bookvolume" placeholder="册数"></el-input>
     </el-form-item>
    </template>
   </el-table-column>
   <el-table-column prop="bookbuyer" label="购买者">
    <template slot-scope="scope">
     <el-form-item :prop="'tableData.' + scope.$index + '.bookbuyer'" :rules='formData.rules.name'>
     <el-input v-model="scope.row.bookbuyer" placeholder="购买者"></el-input>
     </el-form-item>
    </template>
   </el-table-column>
   <el-table-column prop="bookborrower" label="借阅者">
    <template slot-scope="scope">
    <el-form-item :prop="'tableData.' + scope.$index + '.bookborrower'" :rules='formData.rules.name'>
     <el-input v-model="scope.row.bookborrower" placeholder="借阅者"></el-input>
    </el-form-item>
    </template>
   </el-table-column>
   <el-table-column prop="bookbuytime" label="购买日期">
    <template slot-scope="scope">
    <el-form-item :prop="'tableData.' + scope.$index + '.bookbuytime'" :rules='formData.rules.data1'>
     <el-date-picker
     v-model="scope.row.bookbuytime"
     type="date"
     placeholder="购买日期">
     </el-date-picker>
    </el-form-item>
    </template>
   </el-table-column>
   <el-table-column label="操作">
    <template slot-scope="scope">
    <el-button
     size="mini"
     type="danger"
     v-if="!scope.row.editing"
     icon="el-icon-delete"
     @click="handleDelete(scope.$index, scope.row)">删除
    </el-button>
    </template>
   </el-table-column>
  </el-table>
  </el-form>
 </div>
</template>

vuejs 代码

export default {
 name:'TestWorld',
 data() {
  return {
    formData:{
     rules:{
     name:{ 
       type:"string",
       required:true,
       message:"必填字段",
       trigger:"blur"
       },
     volume1:{ 
        type:"number",
        required:true,
        message:"册数必须为数字值",
        trigger:"change"
       },
     data1:{ 
       type:"date",
       required:true,
       message:"请选择日期",
       trigger:"change"
       }
     },
     tableData:[{
     bookname: '',
     bookbuytime: '',
     bookbuyer: '',
     bookborrower: '',
     bookvolume:''
     }]
    }
  }
 },
 methods:{
  addLine(){ //添加行数
   var newValue = {
     bookname: '',
     bookbuytime: '',
     bookbuyer: '',
     bookborrower: '',
     bookvolume:''
    };
   //添加新的行数
   this.formData.tableData.push(newValue);
  },
  handleDelete(index){ //删除行数
   this.formData.tableData.splice(index, 1)
  },
  save(formName){ //保存
   this.$refs[formName].validate((valid,model) => {
    console.log(valid)
    console.log(JSON.stringify(model))
   if (valid) {
    alert('submit!');
   } else {
    console.log('error submit!!');
    return false;
   }
 
   });
  },
  handleDelete(index){ //删除行数
   console.log(index)
   this.formData.tableData.splice(index, 1)
  }
 }
 
}

补充知识:element-ui 跟form 和table 动态表单校验,数组的深层次校验

首先数据结构是这样的

let cchiCombineBill = [
   {
    infoId: '1716',
    clinicCchiCombineName: '星期四',
    clinicCchiCombineId: '3',
    serviceCount: '1',
    cchis: [
     {
      cchiCode: 'CAAJ1000'
     },
     {
      cchiCode: 'CAAJ1400'
     }
    ]
   },
   {
    infoId: '1816',
    clinicCchiCombineName: '星期五',
    clinicCchiCombineId: '3',
    serviceCount: '1',
    cchis: [
     {
      cchiCode: 'CAAJ1000'
     },
     {
      cchiCode: 'CAAJ1400'
     }
    ]
   }
  ]

vue element table中自定义一些input的验证操作

vue element table中自定义一些input的验证操作

<template>
 <div class="bill-wrapper">
  <p class="title-p">费用调整</p>
  <el-divider />
  <el-form ref="mainForm" :model="fromData" class="form-new">
   <section class="pay-section">
    <p class="pay-p">
     <span class="pay-span">医疗服务操作</span>
    </p>
    <div>
     <section v-for="(item ,index) in fromData.cchiCombineBill" :key="index">
      <p class="tip-p">
       {{ item.clinicCchiCombineName }}
       <span class="tip-span">(服务数量:{{ item.serviceCount }})</span>
      </p>
      <el-table :data="item.cchis" border style="width: 100%;">
       <el-table-column prop="cchiCode" label="CCHI 编码" min-width="100" />
       <el-table-column label="调整后支付价格" min-width="160">
        <template slot-scope="scope">
         <el-form-item
          :prop="`cchiCombineBill.${index}.cchis.${scope.$index}.adjustPaymentPrice`"
          :rules="fromData.fromaDataRules.adjustPaymentPrice"
         >
          <el-input v-model="scope.row.adjustPaymentPrice" placeholder="请输入" />
         </el-form-item>
        </template>
       </el-table-column>
      </el-table>
     </section>
    </div>
   </section>
  </el-form>
  <p class="new-p">
   <!-- <el-button type="primary" class="btn" @click="returnFn">返回</el-button> -->
   <el-button type="primary" class="btn" @click="sureFn">保存</el-button>
  </p>
 </div>
</template>
<script>
import { numFixTwo } from '@/utils/tool/regExp'
export default {
 data() {
  const validateNumFixTwo = (rule, value, callback) => {
   if (numFixTwo(value)) {
    callback()
   } else {
    callback(new Error('数字,保留小数点后两位'))
   }
  }
  return {
   fromData: {
    cchiCombineBill: [],
    fromaDataRules: {
     adjustPaymentPrice: [
      { required: true, message: '请输入调整后价格', trigger: 'change' },
      { required: true, trigger: 'change', validator: validateNumFixTwo }
     ]
    }
   }
  }
 },
 created() {
  let cchiCombineBill = [
   {
    infoId: '1716',
    clinicCchiCombineName: '星期四',
    clinicCchiCombineId: '3',
    serviceCount: '1',
    cchis: [
     {
      cchiCode: 'CAAJ1000'
     },
     {
      cchiCode: 'CAAJ1400'
     }
    ]
   },
   {
    infoId: '1816',
    clinicCchiCombineName: '星期五',
    clinicCchiCombineId: '3',
    serviceCount: '1',
    cchis: [
     {
      cchiCode: 'CAAJ1000'
     },
     {
      cchiCode: 'CAAJ1400'
     }
    ]
   }
  ]
  cchiCombineBill.map(item => {
   let cchis = []
   item.cchis.map(item2 => {
    this.$set(item2, 'adjustPaymentPrice', '')
    cchis.push(item2)
   })
   item.cchis = cchis
   this.fromData.cchiCombineBill.push(item)
  })
 },
 methods: {
  getFormPromise(form) {
   return new Promise(resolve => {
    form.validate(res => {
     resolve(res)
    })
   })
  },
  sureFn() {
   const mainForm = this.$refs.mainForm // 用户信息
   Promise.all(
    [mainForm].map(this.getFormPromise) // 校验各个表单是否合格
   ).then(res => {
    const validateResult = res.every(item => !!item)
    if (validateResult) {
     console.log('表单都校验通过')
    } else {
     this.$message({
      message: `填写有误,请检查`,
      type: 'warning'
     })
    }
   })
  }
 }
}
</script>
<style lang="scss" scoped>
.bill-wrapper {
 min-width: 1110px;
 margin: 0 auto;
 padding: 20px;
 /deep/ .el-divider--horizontal {
  margin-top: 8px;
 }
 // /deep/ .el-form-item {
 //  margin-bottom: 30px;
 // }
 .return-p {
  margin-bottom: 20px;
 }
 .new-p {
  margin-top: 40px;
  text-align: center;
  .btn:first-child {
   margin-right: 30px;
  }
 }
 .pay-section {
  margin-top: 50px;
  .pay-p {
   padding-left: 10px;
   // border: 1px solid #e8e8e8;
   height: 30px;
   line-height: 30px;
   font-size: 14px;
   margin-top: 20px;
   background: #409eff;
   color: white;
  }
 }
 .sub-title {
  color: #444;
  margin-top: 30px;
 }
 .tip-p {
  margin-top: 15px;
  color: #409eff;
  font-size: 14px;
  margin-bottom: 5px;
  .tip-span {
   font-size: 12;
  }
 }
}
</style>

之前一直是数组结合table 一层的校验,琢磨了很久才终于领悟 element-ui 的 form表单校验的精髓所在,

那就是 :prop 一定是遍历的数组'cchiCombineBill.' 加上(cchiCombineBill,index)中 的index,再加上具体要校验的字段。

以上这篇vue element table中自定义一些input的验证操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
jquery easyui使用心得
Jul 07 Javascript
javascript根据时间生成m位随机数最大13位
Oct 30 Javascript
node.js中Socket.IO的进阶使用技巧
Nov 04 Javascript
5种处理js跨域问题方法汇总
Dec 04 Javascript
javascript实现博客园页面右下角返回顶部按钮
Feb 22 Javascript
JavaScript设计模式开发中组合模式的使用教程
May 18 Javascript
浅谈JS之iframe中的窗口
Sep 13 Javascript
AngularJS控制器之间的通信方式详解
Nov 03 Javascript
Node.js 8 中的 util.promisify的详解
Jun 12 Javascript
10个最优秀的Node.js MVC框架
Aug 24 Javascript
vue自定义全局组件(自定义插件)的用法
Jan 30 Javascript
Vue.Draggable拖拽功能的配置使用方法
Jul 29 Javascript
vue cli4.0项目引入typescript的方法
Jul 17 #Javascript
js实现省级联动(数据结构优化)
Jul 17 #Javascript
Vue如何基于vue-i18n实现多国语言兼容
Jul 17 #Javascript
jquery实现有过渡效果的tab切换
Jul 17 #jQuery
使用Vue-cli 中为单独页面设置背景图片铺满全屏
Jul 17 #Javascript
vue 点击其他区域关闭自定义div操作
Jul 17 #Javascript
vue v-for出来的列表,点击某个li使得当前被点击的li字体变红操作
Jul 17 #Javascript
You might like
详解js异步文件加载器
2016/01/24 PHP
PHP中的密码加密的解决方案总结
2016/10/26 PHP
PHP编程中的Session阻塞问题与解决方法分析
2017/08/07 PHP
CodeIgniter框架钩子机制实现方法【hooks类】
2018/08/21 PHP
laravel 错误处理,接口错误返回json代码
2019/10/25 PHP
PHP 图片合成、仿微信群头像的方法示例
2019/10/25 PHP
JS实现多物体缓冲运动实例代码
2013/11/29 Javascript
js 获取、清空input type=&quot;file&quot;的值示例代码
2014/02/19 Javascript
用jQuery toggleClass 实现鼠标移上变色
2014/05/14 Javascript
jQuery截取指定长度字符串的实现原理及代码
2014/07/01 Javascript
我的NodeJs学习小结(一)
2014/07/06 NodeJs
jQuery横向擦除焦点图特效代码分享
2015/09/06 Javascript
利用adb shell和node.js实现抖音自动抢红包功能(推荐)
2018/02/22 Javascript
vee-validate vue 2.0自定义表单验证的实例
2018/08/28 Javascript
在vue中实现禁止回退上一步,路由不存历史记录
2020/07/22 Javascript
Vue 数据绑定的原理分析
2020/11/16 Javascript
用Python实现QQ游戏大家来找茬辅助工具
2014/09/14 Python
python基于xmlrpc实现二进制文件传输的方法
2015/06/02 Python
python简单判断序列是否为空的方法
2015/06/30 Python
Python调用SQLPlus来操作和解析Oracle数据库的方法
2016/04/09 Python
python操作 hbase 数据的方法
2016/12/18 Python
基于python OpenCV实现动态人脸检测
2018/05/25 Python
详细介绍pandas的DataFrame的append方法使用
2019/07/31 Python
Python字典底层实现原理详解
2019/12/18 Python
Python如何生成xml文件
2020/06/04 Python
python+selenium+chrome实现淘宝购物车秒杀自动结算
2021/01/07 Python
H5 canvas中width、height和style的宽高区别详解
2018/11/02 HTML / CSS
岳父生日宴会答谢词
2014/01/13 职场文书
五四青年节的活动方案
2014/08/20 职场文书
甲乙双方合作协议书
2014/10/13 职场文书
2015年国庆节标语大全
2015/07/30 职场文书
Python代码,能玩30多款童年游戏!这些有几个是你玩过的
2021/04/27 Python
Pytorch 统计模型参数量的操作 param.numel()
2021/05/13 Python
JavaWeb 入门篇(3)ServletContext 详解 具体应用
2021/07/16 Java/Android
彻底解决MySQL使用中文乱码的方法
2022/01/22 MySQL
Python 统计序列中元素的出现频度
2022/04/26 Python