ElementUI的动态编辑标签默认只有添加,现在我要有修改的功能

1、要有添加修改的功能

2、要实现去重,不管是添加,还是修改,如果重复的话,都应该进行提示

3、需求还要实现拖拽排序功能,正在实现中。

就是实现有一个按钮,当年点击时,他会变成一个输入框,你可以输入值,失去焦点后,他就又变成一个标签,然后标签后面会有编辑和删除按钮,点击编辑可以修改当前标签的值。

效果

1、初始效果

 2、点击新增时 

 3、点击编辑时

 4、重复时

HTML

 <el-tag :key="tag" type="info" v-for="tag in dynamicTags" closable :disable- 
          transitions="false"  @close="handleClose(tag)" class="distanceTag">
     <span style="margin-right: 4px;">{{ tag }}</span>
     <i class="el-icon-edit" @click="changeValue(tag)"></i>
  </el-tag>

<el-input  v-if="inputVisible" v-model="inputValue" ref="saveTagInput"
          size="small" @keyup.enter.native="handleInputConfirm" @blur="handleInputConfirm">
 </el-input>
 <el-button v-else class=" assortAdd" size="small" border-style="['dashed']"
              :border-color="['#1F64E3']" @click="showInput">+新增</el-button>

JS

data(){
  return{
        dynamicTags: ['标签一', '标签二', '标签三'],
        inputVisible: false,
        inputValue: '',
        tempTag: '',
        // 是否是重复数据
        isRepeatedData: false,
        // 是否改变原来的值
        isChange: false,
     
}},
methods:{
 handleClose(tag) {
        this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
      },

      showInput() {
        this.tempTag = ''
        this.inputVisible = true;
        this.inputValue = ''
        this.isChange = false

        this.$nextTick(_ => {
          this.$refs.saveTagInput.$refs.input.focus();
        });
      },

      handleInputConfirm() {
        this.isRepeatedData = false
        let inputValue = this.inputValue;
        // 去空格
        inputValue = inputValue.replace(/^\s\s*/, '').replace(/\s\s*$/, '')
        if (inputValue == '') {
          this.inputVisible = false
          return
        }
        // 判断新增的值是否重复
        console.log(this.dynamicTags.indexOf(inputValue),this.tempTag,inputValue);
        const isDuplicate = this.dynamicTags.some((tag) => tag === inputValue);
        if (isDuplicate && this.tempTag !== inputValue) {
          this.isRepeatedData = true
          this.$message.error("不允许添加重复数据!")
          return
        } else {
          this.isRepeatedData = false
        }
        if (this.isChange) {
        const index = this.dynamicTags.indexOf(this.tempTag);
    if (index !== -1) {
      this.dynamicTags.splice(index, 1, inputValue);
    }
    this.inputVisible = false;
    return;
        }
        // 点击添加时,追加
        if (inputValue) {
          this.dynamicTags.push(inputValue);
          console.log(inputValue+'tt:'+this.dynamicTags)
        }
        this.inputVisible = false;
        this.inputValue = '';
      },
      changeValue(tag) {
        console.log(tag,'tag');
        this.inputVisible = true
       
        this.inputValue = tag
        this.tempTag = tag
        this.isChange = true
        this.$nextTick(_ => {
          this.$refs.saveTagInput.$refs.input.focus();
        });
      },

}

样式根据需求自己写吧

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

起名时在学Aiifox

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值