element中el-table中的el-input校验的实现

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>

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值