element-ui动态编辑标签

在这里插入图片描述
点击叉叉,标签消失,点击New Tag,显示输入框,输入完成后生成标签,并且出现New Tag标签。

代码:

<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>

通过v-for,根据description的内容进行渲染。当点击New Tag后,通过v-if和v-else来显示输入框,并去掉New Tag,

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

来获取到焦点
在完成输入后由于绑定了失去焦点的事件和回车按下后的事件,会将新的内容放入数组中,通过v-for重新进行渲染,并且重置相关值以消失。
关闭标签则是直接对数组进行剪切,删除某个值,再通过v-for重新进行渲染。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
element-ui中的动态编辑标签实现可以借助el-tag组件和el-input组件,具体实现步骤如下: 1.使用el-tag组件展示标签,并在标签上绑定点击事件,点击标签时触发编辑模式。 2.在编辑模式下,使用el-input组件替换原标签编辑完成后再将el-input组件替换回el-tag组件。 3.在el-input组件上绑定blur事件,在失去焦点时触发编辑完成事件,并将编辑的内容传递给父组件进行保存。 以下是示例代码: ```html <template> <el-tag v-if="!isEdit" @click="handleEdit">{{ value }}</el-tag> <el-input v-else v-model="editValue" @blur="handleEditComplete"></el-input> </template> <script> export default { props: { value: { type: String, required: true } }, data() { return { isEdit: false, editValue: this.value } }, methods: { handleEdit() { this.isEdit = true; this.$nextTick(() => { this.$refs.input.focus(); }) }, handleEditComplete() { if (this.editValue !== this.value) { this.$emit('update:value', this.editValue); } this.isEdit = false; } } } </script> ``` 在父组件中使用该组件时,只需将需要编辑标签内容通过value属性传递给该组件,并监听update:value事件获取编辑完成后的内容即可。 ```html <template> <dynamic-tag :value="tag" @update:value="handleTagUpdate" /> </template> <script> import DynamicTag from '@/components/DynamicTag.vue' export default { components: { DynamicTag }, data() { return { tag: '标签名' } }, methods: { handleTagUpdate(value) { this.tag = value; } } } </script> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值