当前位置: 首页 > article >正文

Vue+ElementUI 字符串数组标签化展示组件

一. 效果

数据:‘[“苹果”,“香蕉”]’
效果
在这里插入图片描述

可添加,编辑,删除。

二. 组件源码

<template>
  <div>
    <div v-for="(item, index) in items"
         :key="index">
      <el-input
        v-if="inputVisible && editingIndex === index"
        ref="input"
        v-model="inputValue"
        size="mini"
        @keyup.enter.native="handleInputConfirm"
        @blur="handleInputConfirm"
      />
      <el-tag
        v-else
        closable
        disable-transitions
        @close="removeTag(index)"
        @click="editTag(index)"
      >
        {{ item }}
      </el-tag>
    </div>
    <el-input
      v-if="inputVisible && editingIndex === null"
      ref="input"
      v-model="inputValue"
      size="mini"
      @keyup.enter.native="handleInputConfirm"
      @blur="handleInputConfirm"
    />
    <el-button v-else size="small" @click="showInput">+ 添加</el-button>
  </div>
</template>

<script>
export default {
  name: "EditableTags",
  props: {
    // 接收一个 JSON 数组字符串作为初始值
    initialItems: {
      type: String,
      default: '[]'
    }
  },
  data() {
    return {
      // 解析初始值
      items: JSON.parse(this.initialItems),
      inputVisible: false,
      inputValue: '',
      editingIndex: null
    };
  },
  watch: {
    // 监听 initialItems 的变化
    initialItems: {
      handler(newItems) {
        this.items = JSON.parse(newItems);
      },
      immediate: true
    },
    // 监听 items 的变化,触发父组件的更新事件
    items: {
      handler(newItems) {
        this.$emit('update:items', JSON.stringify(newItems));
      },
      deep: true
    }
  },
  methods: {
    removeTag(index) {
      this.items.splice(index, 1);
    },
    editTag(index) {
      this.editingIndex = index;
      this.inputValue = this.items[index];
      this.inputVisible = true;
      this.$nextTick(() => {
        this.$refs.input[0].focus();
      });
    },
    showInput() {
      this.inputVisible = true;
      this.$nextTick(() => {
        this.$refs.input.focus();
      });
    },
    handleInputConfirm() {
      if (this.inputValue) {
        if (this.editingIndex !== null) {
          this.items[this.editingIndex] = this.inputValue;
          // 由于watch监控不到数组元素值的变化, 所以手动通知
          this.$set(this.items, this.editingIndex, this.inputValue);
          this.editingIndex = null;
        } else {
          this.items.push(this.inputValue);
        }
      }
      this.inputVisible = false;
      this.inputValue = '';
    }
  }
};
</script>

<style scoped>
.el-tag {
  margin-right: 10px;
}
</style>

三. 使用组件

<template>
  <editable-tags :initial-items='items' @update:items="newItems => items = newItems" />
</template>

<script>
import EditableTags from "editableTags";

export default {
  components: {
    EditableTags
  },
  data() {
    return {
      items: '["苹果","香蕉"]',
    }
  }
};
</script>


http://www.kler.cn/a/600149.html

相关文章:

  • 07_GRU模型
  • 好好学Docker:基于Docker buildx构建多平台镜像【转载】
  • 本地部署Stable Diffusion生成爆火的AI图片
  • (UI自动化测试web端)第二篇:元素定位的方法_xpath扩展(工作当中用的比较多)
  • Python022(字典02)
  • 欢迎来到未来:探索 Dify 开源大语言模型应用开发平台
  • eclipse [jvm memory monitor] SHOW_MEMORY_MONITOR=true
  • spring-security原理与应用系列:总体流程
  • RabbitMQ的高级特性介绍(二)
  • Transformer 通关秘籍2:利用 BERT 将文本 token 化
  • 基于微信小程序的短文写作竞赛管理系统
  • Windows桌面采集技术
  • 【Matlab】串口通信(serialport对象,读写、回调、删除等)
  • Java-腾讯云短信模板兼容阿里云短信模板-短信模板参数生成
  • 【JavaWeb学习Day27】
  • Windows下编译安装Qt5.15.0指南
  • 23种设计模式中的策略模式
  • Xshell、Xsftp、Xmanager中文版安装包及使用教程
  • Redis Sentinel(哨兵模式)高可用性解决方案
  • hackmyvm-Icecream