element-ui 解决table里包含表单验证

实际项目中的场景,需要在table里做表单验证,如下图所示

在这里插入图片描述
其实问题关键就在于如何给el-form-item动态绑定prop

      :prop="'phones.' + scope.$index + '.字段名'"
<el-form :model="dataForm" ref="dataForm" :rules="rules">
        <el-table :data="dataForm.phones" border stripe :index="true" ref="dataList" max-height="400">
          <el-table-column label="序号" type="index" width="50"></el-table-column>
          <el-table-column prop="name" label="姓名" header-align="center" align="center" width="120">
            <template slot-scope="scope">
              <el-col >
              <el-form-item :prop="'phones.'+ scope.$index + '.name'" 
              :rules="phonesRules.name">
                <el-input v-model="scope.row.name" placeholder="姓名"></el-input>
              </el-form-item>
              </el-col>
            </template>
          </el-table-column>
          <el-table-column prop="idCard" label="身份证号码" header-align="center" align="center" width="220">
            <template slot-scope="scope">
              <el-form-item :prop="'phones.'+ scope.$index + '.idCard'" 
              :rules="phonesRules.idCard">
                <el-input v-model="scope.row.idCard" placeholder="身份证号码" maxlength="18"></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column prop="phone" label="联系电话" header-align="center" align="center" width="200">
            <template slot-scope="scope">
              <el-form-item :prop="'phones.'+ scope.$index + '.phone'" 
              :rules="phonesRules.phone">
                <el-input v-model="scope.row.phone" placeholder="联系电话" maxlength="11"></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column prop="project" label="培训项目" header-align="center" align="center" width="160">
            <template slot-scope="scope">
              <el-form-item :prop="'phones.'+ scope.$index + '.project'" 
              :rules="phonesRules.project">
                <el-input v-model="scope.row.project" placeholder="培训项目"></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column prop="spotTm" label="电话抽查时间" header-align="center" align="center" width="200">
            <template slot-scope="scope">
              <el-form-item :prop="'phones.'+ scope.$index + '.spotTm'" 
              :rules="phonesRules.spotTm">
                <el-date-picker v-model="scope.row.spotTm" placeholder="时间"></el-date-picker>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="操作" fixed="right" header-align="center" align="center" width="50">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="delHandle(scope.$index)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
    </el-form>
</template>
 phonesRules:{
          name: [
            { required: true, message: '请输入姓名', trigger: 'blur' }
          ],
          idCard: [
            { required: true, message: '请输入身份证号码', trigger: 'blur' },
            {
              pattern:/^\d{15}|\d{18}$/,
              message: "身份证号码格式错误",
              trigger: "blur"
            }
          ],
          phone: [
            { required: true, message: '请输入联系电话', trigger: 'blur' },
            {
              pattern:/^1[0-9]{10}$/,
              message: "联系电话格式不正确",
              trigger: "blur"
            }
          ],
          project: [
            { required: true, message: '请输入培训项目', trigger: 'blur' }
          ],
          spotTm: [
            { required: true, message: '请选择电话抽查时间', trigger: 'blur' }
          ],
        },

遇到了因为浏览器问题的原因,导致table的样式有问题,增加以下代码解决。

<style lang="scss" >
  .elTable{
    .el-table .cell, .el-table th div{
      white-space: normal!important;
    }
  }
</style>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值