el-table中的el-input校验
bidepanm 人气:0本文主要介绍了element中el-table中的el-input校验的实现,具体如下:
<el-form :model="formParams" :rules="rules" ref="ruleForm" label-width="0"> <el-tabs v-model="activeName" type="card" @tab-click="changeTab"> <el-tab-pane v-for="item in tabList" :name="item.name" :key="item.id"> <div slot="label"> {{item.name}}({{totalCount[item.name] || 0}}) </div> <el-table v-show="activeName==='xxx'" :row-class-name="tableRowClass" :data="formParams.xxxData" border> <el-table-column min-width="10%" prop="num" label="数量"> <template slot-scope="scope"> <el-form-item :prop="'xxxData.' + scope.$index + '.num'" :rules="rules.num"> <el-input v-model="scope.row.num" maxlength="6" @input="value => scope.row.num= Number(value.replace(/[^\d]/g,''))" size="small"></el-input> </el-form-item> </template> </el-table-column> <el-table-column min-width="20%" label="时间"> <template slot-scope="scope"> <el-time-picker style="width: 45%" v-model="scope.row.startTime" value-format="HH:mm:ss" :picker-options="{ selectableRange: '00:00:00 - 12:59:59' }" size="small" placeholder="开始时间"> </el-time-picker> - <el-time-picker style="width: 45%" v-model="scope.row.endTime" value-format="HH:mm:ss" :picker-options="{ selectableRange: `${scope.row.startTime ? scope.row.startTime : '00:00:00'}-12:59:59`, }" size="small" placeholder="结束时间"> </el-time-picker> </template> </el-table-column> <el-table-column min-width="10%" label="操作"> <template slot-scope="scope"> <a @click="delete(scope.$index)">删除</a> </template> </el-table-column> </el-table> </el-tab-pane> </el-tabs> </el-form>
1. 点击保存的时候校验num
data() { return { num: [ { required: true, message: '请输入数量', trigger: 'change' }, ] } }, methods: { submitForm(formName) { this.$refs[formName].validate(valid => { if (valid) { alert("submit!"); } else { return false; } }); } }
2. 由于每个tab页对应展示不同的数据列表,并且每个列表可以添加一条新的数据,如果想在保存时提示具体信息,如果"xxx的数量不能为空",“yyy的数量不能为空”,可以在点击保存时对不同的数据列表进行循环
this.validateNum(this.formParams.xxxData, 'xxx'); this.validateNum(this.formParams.yyyData, 'yyy'); validateNum(list, msg) { if (list && list.length && list.findIndex(item => item.num === '') !== -1) { this.tips.push(msg); } } if (this.tips.length) { message += `${this.tips.join('、')}的数量不能为空;`; }
3. 如果把<el-form>放在<el-tab>循环里面,在v-for循环中使用form表单验证this.$refs[formName].validate会出现错误TypeError: this.$refs[formName].validate is not a function:
由于this.$refs[formName]是一个数组,使用this.$refs[formName][0].validate((valid) => {}
4. time-picker中想要设置结束时间大于开始时间
selectableRange: `${scope.row.startTime ? scope.row.startTime : '00:00:00'}-12:59:59`
5. 给el-table中的指定行指定特殊的样式
tableRowClass(val) { if (val.row.type === 'xxxxxx') { return 'row-disable'; } else { return ''; } }
6. el-input中限制只能输入数字
<el-input v-model="count" @input="value => count = Number(value.replace(/[^\d]/g,''))" </el-input>
加载全部内容