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 相关文章推荐
js资料toString 方法
Mar 13 Javascript
json 实例详细说明教程
Oct 31 Javascript
iframe的onload在Chrome/Opera中执行两次Bug的解决方法
Mar 17 Javascript
Javascript浅谈之this
Dec 17 Javascript
js获取select选中的option的text示例代码
Dec 19 Javascript
JS生成不重复随机数组的函数代码
Jun 10 Javascript
js QQ客服悬浮效果实现代码
Dec 12 Javascript
获取JS中网页各种高宽与位置的方法总结
Jul 27 Javascript
深入理解Vue生命周期、手动挂载及挂载子组件
Sep 27 Javascript
JS实现同一DOM元素上onClick事件与onDblClick事件并存的解决方法
Jun 07 Javascript
图文讲解vue的v-if使用方法
Feb 11 Javascript
使用vue实现各类弹出框组件
Jul 03 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
ThinkPHP关联模型操作实例分析
2012/09/23 PHP
linux实现php定时执行cron任务详解
2013/12/24 PHP
PHP设计模式之装饰者模式代码实例
2015/05/11 PHP
php简单实现发送带附件的邮件
2015/06/10 PHP
WordPress中转义HTML与过滤链接的相关PHP函数使用解析
2015/12/22 PHP
php 常用的系统函数
2017/02/07 PHP
laravel5.4利用163邮箱发送邮件的步骤详解
2017/09/22 PHP
js下通过getList函数实现分页效果的代码
2010/09/17 Javascript
jquery 与NVelocity 产生冲突的解决方法
2011/06/13 Javascript
JS输入用户名自动显示邮箱后缀列表的方法
2015/01/27 Javascript
jQuery图片前后对比插件beforeAfter用法示例【附demo源码下载】
2016/09/20 Javascript
JS实现图文并茂的tab选项卡效果示例【附demo源码下载】
2016/09/21 Javascript
request请求获取参数的实现方法(post和get两种方式)
2016/09/27 Javascript
Bootstrap3 多选和单选框(checkbox)
2016/12/29 Javascript
javascript添加前置0(补零)的几种方法
2017/01/05 Javascript
纯jQuery实现前端分页功能
2017/03/23 jQuery
深入理解vue $refs的基本用法
2017/07/13 Javascript
详解如何在react中搭建d3力导向图
2018/01/12 Javascript
AngularJS实现动态切换样式的方法分析
2018/06/26 Javascript
解决vuex数据异步造成初始化的时候没值报错问题
2019/11/13 Javascript
如何在postman测试用例中实现断言过程解析
2020/07/09 Javascript
vue 监听 Treeselect 选择项的改变操作
2020/08/31 Javascript
Python数据分析之获取双色球历史信息的方法示例
2018/02/03 Python
Django打印出在数据库中执行的语句问题
2019/07/25 Python
python数据预处理方式 :数据降维
2020/02/24 Python
Python实现手绘图效果实例分享
2020/07/22 Python
Python数据库封装实现代码示例解析
2020/09/05 Python
Python爬虫自动化爬取b站实时弹幕实例方法
2021/01/26 Python
伦敦香水公司:The London Perfume Company
2019/11/13 全球购物
高分子材料个人求职信范文
2013/09/25 职场文书
会计实习生工作总结的自我评价
2013/10/07 职场文书
项目投资意向书
2014/04/01 职场文书
幼儿老师求职信
2014/06/30 职场文书
行政管理专业求职信
2014/07/06 职场文书
幼儿园中班个人总结
2015/02/28 职场文书
2016基督教会圣诞节开幕词
2016/03/04 职场文书