el-table嵌套 el-form并且加正则校验

el-table 嵌套 el-form表单 并且进行正则校验

  1. 上图
    在这里插入图片描述
    在这里插入图片描述

  2. 上代码

<template>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0" class="demo-ruleForm">
      <!-- 这里用的封装的表格  表格组件 放在最下面-->
        <table-operation
          class="mp-custom-table"
          :tableData="ruleForm.tableData"
          :theadData="option"
          ref="tableRef"
        >
          <template slot="ipAddress" slot-scope="scope">
          <!-- 此处prop必须设置一个动态的值-->
            <el-form-item :prop="propIpAddress(tableData, scope)" :rules="rules.ipAddress" style="margin-bottom: 0px; width: 98%" class="form-text">
              <span class="required-label">*</span>
              <el-input style="width: 90%" v-model="scope.row.ipAddress" placeholder="请输入IP地址"></el-input>
            </el-form-item>
          </template>
          <template slot="macAddress" slot-scope="scope">
           <!-- 此处prop必须设置一个动态的值-->
            <el-form-item style="margin-bottom: 0px; width: 80%" :prop="propMacAddress(tableData, scope)" class="form-text" :rules="rules.macAddress">
              <el-input v-model="scope.row.macAddress" placeholder="请输入Mac地址"></el-input>
            </el-form-item>
          </template>
        </table-operation>
      </el-form>
</template>
<script>
import TableOperation from '@/custom/tableList'; // 表格公用组件
import { goLiveHeader } from '../assetAccountConfig.js';
export default {
  components: {
    TableOperation, // 表格公用组件
  },
  data() {
    var validateIP = (rule, value, callback) => {
      if (value) {
        const reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
        if (!reg.test(value) && value != '') {
          callback(new Error('请输入正确的IP地址'));
        } else {
          callback();
        }
      }
    };
    var validateMac = (rule, value, callback) => {
      if (value) {
        let reg1 = /^[A-Fa-f0-9]{1,2}-[A-Fa-f0-9]{1,2}-[A-Fa-f0-9]{1,2}-[A-Fa-f0-9]{1,2}-[A-Fa-f0-9]{1,2}-[A-Fa-f0-9]{1,2}$/;
        let reg2 = /^[A-Fa-f0-9]{1,2}:[A-Fa-f0-9]{1,2}:[A-Fa-f0-9]{1,2}:[A-Fa-f0-9]{1,2}:[A-Fa-f0-9]{1,2}:[A-Fa-f0-9]{1,2}$/;
        if (reg1.test(value)) {
          callback();
        } else if (reg2.test(value)) {
          callback();
        } else {
          callback(new Error('请输入正确的Mac地址'));
        }
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        tableData: [], //表格数据
      },
      rules: {
        ipAddress: [
          { required: true, message: '请输入IP地址', trigger: 'blur' },
          { validator: validateIP, trigger: 'blur' },
        ],
        macAddress: [{ validator: validateMac, trigger: 'blur' }],
      }
    };
  },
  methods: {
    propIpAddress(data, index) {
      // 此处我用的封装表格 所以 取值为 index.scope.$index 
      // 请根据自己的实际数据 取值
      return 'tableData.' + index.scope.$index + '.ipAddress';
    },
    propMacAddress(data, index) {
      return 'tableData.' + index.scope.$index + '.macAddress';
    },
  },
};
</script>
  1. 完成 欧克!
  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值