ElementUI Tag组件实现多标签生成

现在好多应用场景里会有一些需要给文章打标签等类似的操作,之前jquery用户是使用taginput来实现,使用VUE以后elementui有一个组件非常简单就是tag组件。


image.png
<el-tag
  :key="tag"
  v-for="tag in dynamicTags"
  closable
  :disable-transitions="false"
  @close="handleClose(tag)">
  {{tag}}
</el-tag>
<el-input
  class="input-new-tag"
  v-if="inputVisible"
  v-model="inputValue"
  ref="saveTagInput"
  size="small"
  @keyup.enter.native="handleInputConfirm"
  @blur="handleInputConfirm"
>
</el-input>
<el-button v-else class="button-new-tag" size="small" @click="showInput">+ New Tag</el-button>

<style>
  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
</style>

<script>
  export default {
    data() {
      return {
        dynamicTags: ['标签一', '标签二', '标签三'],
        inputVisible: false,
        inputValue: ''
      };
    },
    methods: {
      handleClose(tag) {
        this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
      },

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

      handleInputConfirm() {
        let inputValue = this.inputValue;
        if (inputValue) {
          this.dynamicTags.push(inputValue);
        }
        this.inputVisible = false;
        this.inputValue = '';
      }
    }
  }
</script>

这个是官方文档给的实例,这样可以解决单一标签输入。但是实际场景中,好多用户是通过ctrl+c,ctrl+v的方式输入的,有可能还会一起粘贴好多行的标签,更有可能从excel中复制出来。
那我一一解决一下这样一个场景
首先,先改一下样式,让文本框变长:

.el-tag{
  margin-right: 10px;
}
.el-tag + .el-tag {
    margin-right: 10px;
  }
  .button-new-tag {
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    vertical-align: bottom;
  }

接着,修改一下enter和blur事件:

handleInputConfirm() {
  let inputValue = this.inputValue;
    if (inputValue) {
      var values = inputValue.split(/[,, \n]/).filter(item=>{
        return item!='' && item!=undefined
      })
      values.forEach(element => {
        var index = this.dynamicTags.findIndex(i=>{
        return i==element
      })
      if(index<0){
       this.dynamicTags.push(element);
      }
    });      
  }
  this.inputVisible = false;
  this.inputValue = '';
}

效果:

阿大发
asd 

三大发舒服,

阿斯顿发撒地方。
阿斯顿发,阿斯顿发,,阿斯顿发,,阿斯顿发安抚,阿斯顿发 是淡淡的  点点滴滴方法,阿斯顿发撒地方,adfasd

我们把以上文字复制粘贴进去


image.png
image.png

所有去重,拆分都OK,那们在试一下,从excel中复制


image.png
image.png

完成。希望能够帮到有需要的朋友。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,813评论 25 708
  • 这个就是电脑上键盘所有键的功能。 saber_7755 CTRL组合键 Ctrl + A 全选 Ctrl + B ...
    皧烁宝贝阅读 17,496评论 0 33
  • HTML标签解释大全 一、HTML标记 标签:!DOCTYPE 说明:指定了 HTML 文档遵循的文档类型定义(D...
    米塔塔阅读 3,301评论 1 41
  • 我第一次见他照片,是在有他专栏的杂志上面,八卦的那一部分。 清秀少年的模样,嘴里含一颗棒棒糖,天真无邪。那时候《小...
    何子初阅读 380评论 1 0
  • 武德九年八月初九甲子日,李唐帝国皇帝李渊退位,禅位于李世民。李世民登基为帝,次年改元贞观。这一年李唐帝国第一个治世...
    夜已空阅读 652评论 1 4