Vue el-tag实现标签动态编辑

31 篇文章 3 订阅
本文介绍如何使用Element UI实现在前端和后端环境中动态创建、编辑和删除标签,涵盖了el-input、el-tag和v-model的配合,适合开发人员快速掌握前端表单和后端实时同步的标签管理技巧。
摘要由CSDN通过智能技术生成

el-tag实现动态编辑

页面部分:

<el-form-item prop="label">
  <div class="new-tag">
    <el-input
      ref="saveTagInput"
      v-model.trim="inputValue"
      class="input-new-tag"
      placeholder="请输入标签"
      @keyup.enter.native="handleInputConfirm"
    />
    <el-button icon="el-icon-plus" class="button-new-tag" size="small" @click="handleInputConfirm">添加</el-button>
  </div>

  <div v-for="(tag, index) in dynamicTags" :key="index" class="tags-inline">
    <el-input
      v-if="editable[index]"
      :ref="'editableInput' + index"
      v-model="tag.label"
      class="input-edit-tag"
      size="small"
      placeholder="请输入标签"
      maxlength="100"
      @keyup.enter.native="handleEditableInputConfirm(tag, index)"
      @change="handleEditableInputConfirm(tag, index)"
      @blur="handleEditableInputBlur(tag, index)"
    />
    <el-tag
      v-else
      closable
      :disable-transitions="false"
      @click="showEditTagInput(index)"
      @close="handleClose(tag, index)"
    >{{ tag.label }}</el-tag>
  </div>
</el-form-item>

script部分:

export default {
  data() {
    return {
      dynamicTags: [],
      inputVisible: false,
      inputValue: '',
      editable: []
    }
  }methods: {
    // 确认添加标签
    handleInputConfirm() {
      const inputValue = this.inputValue
      if (inputValue) {
        this.dynamicTags.push({ label: inputValue })
      }
      this.inputVisible = false
      this.inputValue = ''
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },
    // 编辑 标签 input显示
    showEditTagInput(index) {
      this.$set(this.editable, index, true)
      this.$nextTick((_) => {
        const editableInput = 'editableInput' + index
        this.$refs[editableInput][0].$refs.input.focus()
      })
    },
    // 编辑 标签 input发生改变
    handleEditableInputConfirm(item, index) {
      if (item.label) {
        this.$set(this.editable, index, false)
      } else {
        this.$message({ message: '请输入标签', type: 'info' })
      }
    },
    // 编辑 标签 input失去焦点
    handleEditableInputBlur(item, index) {
      this.$set(this.editable, index, false)
    },
    // 删除 标签
    handleClose(tag, index) {
      this.dynamicTags.splice(index, 1)
    }
  }
}
  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值