效果图,在新增行时增加空值判断,如果有空值不允许新增行
vue2代码示例
<template> <div class="home"> <el-form :model="form" ref="form" :rules="rules"> <el-table :data="form.tableData" style="width: 100%"> <el-table-column label="日期" width="180"> <template slot-scope="scope"> <el-form-item :prop="`tableData.${scope.$index}.date`" :rules="rules.date"> <el-input v-model="scope.row.date"></el-input> </el-form-item> </template> </el-table-column> <el-table-column prop="name" label="姓名" width="180"> <template slot-scope="scope"> <el-form-item :prop="`tableData.${scope.$index}.name`" :rules="rules.name"> <el-input v-model="scope.row.name"></el-input> </el-form-item> </template> </el-table-column> <el-table-column prop="address" label="地址"> <template slot-scope="scope"> <el-form-item :prop="`tableData.${scope.$index}.address`" :rules="rules.address"> <el-input v-model="scope.row.address"></el-input> </el-form-item> </template> </el-table-column> </el-table> </el-form> <el-button @click="validForm">新增校验</el-button> </div> </template> <script> // @ is an alias to /src export default { name: 'HomeView', components: {}, data () { return { rules: { date: [ { required: true, message: '请选择日期', trigger: ['change', 'input'] } ], name: [ { required: true, message: '请选择名称', trigger: ['change', 'input'] } ], address: [ { required: true, message: '请选择地址', trigger: ['change', 'input'] } ] }, form: { tableData: [] } } }, methods: { validForm () { console.log(this.form) this.$refs.form.validate(valid => { if (valid) { this.form.tableData.push({}) } }) } }, created () { this.form.tableData = [ { date: '2016-05-04', name: '王小虎', address: '上海市普陀区金沙江路 1517 弄' }, { date: '2016-05-01', name: '王小虎', address: '上海市普陀区金沙江路 1519 弄' }, { date: '2016-05-03', name: '王小虎', address: '上海市普陀区金沙江路 1516 弄' } ] } } </script>
标签:name,form,王小虎,demo,普陀区,校验,行时,address,date From: https://www.cnblogs.com/harryzong/p/18631029