Vue+ElementUI实现点击加号动态添加

1.需求是点击加号图标往下增加,最多增加5行,最少显示一行(每条前面输入框不能为空及每条的前面输入框的值不能重复)

2.具体实现代码

<cbf-form-item label="位置长度" prop="textReplacement" label-width="80px"             
 class="PRuleList11">
        <div v-for="(item,index) in textReplacementList" :key="index">
          <cbf-input placeholder='请输入内容' :maxlength="10" v-model="item.sourceText" 
           class="width150 mr10" size="small"></cbf-input>
          <span>替换为</span>
          <cbf-input v-model="item.replacedText" placeholder='请输入内容,可为空' 
           :maxlength="10" class="width150 ml10 mr10" size="small"></cbf-input>

          <i class="iOscar iOscar-tianjia mr10" style="color: #409eff" @click="addDataTxt">            
          </i>
          <i class="iOscar iOscar-shanchu ml10" @click="delDataTxt(index)" v-if="index !=0" 
           style="color: #ebb563"></i>
        </div>
</cbf-form-item>

2-1 data定义  
    textReplacementList: [
      {
          sourceText: '',
          replacedText: '',
          order: ''
       }
     ],
2-2 rules配置
textReplacement: [{ required: true, trigger: "change", validator: this.rulesTxt }]

2-3 方法
rulesTxt(str, value, cllback) {
    let tempArray = [];
    if (this.formData.ruleType == 7) {
        for (let i = 0; i < this.textReplacementList.length; i++) {
          if (this.textReplacementList[i].sourceText === "") {
            cllback(new Error("替换前文本不得为空"));
            return;
          }
          // // 判断是否重复
          if (tempArray.includes(this.textReplacementList[i].sourceText)) {
            cllback(new Error("替换前文本不能重复"));
            return;
          }
          tempArray.push(this.textReplacementList[i].sourceText);
        }
    }
    cllback();
},
//添加
addDataTxt() {
   if (!this.textReplacementList) {
     this.$set(this.textReplacementList, []);
   }
   if (this.textReplacementList.length == 5) {
     return;
   }
   this.textReplacementList.push({
     sourceText: '',
     replacedText: '',
     order: ''
    });
},
//删除
delDataTxt(index) {
   this.textReplacementList.splice(index, 1);
},

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值