1 Star 2 Fork 0

Lucson/vue2-tag

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

一个基于Vue2的动态添加删除Tag标签的组件

安装

npm install vue2-tag

引入

import Tag from "vue2-tag"
Vue.use(Tag)

组件使用

基础使用

template
<tag
       :tags="sizeForm.tags"
       :add-tag="addTag"
       :delete-tag="deleteTag" 
></tag>
script
<script>
export default {,
  data() {
    return {
      sizeForm: {
        // 数据源  
        tags: [],
      },
    };
  },
  methods: {
    // 添加 tag
    addTag(tag, callback) {
      if (!tag) {
        // 当重复添加时调用这个回调用来提示用户  
        callback(() => {
          // 这里的提示信息可以按照您使用的UI框架决定  
          this.$message({
            type: "error",
            message: `标签不能重复`,
          });
        });
      } else {
        // 将新增的tag追加到数组中  
        this.sizeForm.tags.push(tag);
      }
    },
    // 删除 tag
    deleteTag(tagIndex) {
      this.sizeForm.tags.splice(tagIndex, 1);
    },
  },
};
</script>

上面所演示的是必选项 如果不传递会报错提示相关信息

自定义提示文本

如果需要修改默认的提示文本可以通过 tip-text 属性来修改

<tag
    :tags="sizeForm.tags"
    :add-tag="addTag"
    :delete-tag="deleteTag" 
    :tip-text="这里是自定义的提示文本" 
></tag>

同时也可以接受一个函数 函数的形参为可以输入的最大的标签数量 默认是 7

<tag
    :tags="sizeForm.tags"
    :add-tag="addTag"
    :delete-tag="deleteTag" 
    :tip-text="customTipText"
></tag>
script
<script>
export default {,
  data() {
    return {
 	// ... 省略上面写过的JS代码	
    };
  },
  methods: {
	 // ... 省略上面写过的JS代码
     customTipText(maxTagNumber){
        return "这是自定义的内容,最多可以添加" + maxTagNumber + "个标签"
     } 
  },
};
</script>

修改可以添加的最大标签数量

通过 max-tag-number 属性就可以修改 , 超过 最大数量就添加不进去了

<tag
    :tags="sizeForm.tags"
    :add-tag="addTag"
    :delete-tag="deleteTag" 
    :tip-text="customTipText"
    :max-tag-number="5"
></tag>

修改边框颜色,边框圆角

可以通过 border-colorborder-radius 两个属性来修改

<tag
    :tags="sizeForm.tags"
    :add-tag="addTag"
    :delete-tag="deleteTag" 
    :tip-text="customTipText"
    :max-tag-number="10"
    border-color="#ccc"
    border-radius="2"
></tag>

去除 tag 标签的圆角效果

可以通过 no-tag-rounded 属性来去除

<tag
    :tags="sizeForm.tags"
    :add-tag="addTag"
    :delete-tag="deleteTag" 
    :tip-text="customTipText"
    :max-tag-number="10"
    border-color="red"
    border-radius="25"
    no-tag-rounded
></tag>

修改 tag 标签的背景,字体颜色

通过 tag-background-color tag-text-color 修改

<tag
    :tags="sizeForm.tags"
    :add-tag="addTag"
    :delete-tag="deleteTag"
    :tip-text="customTipText"
    :max-tag-number="10"
    border-color="red"
    border-radius="25"
    no-tip-rounded
    tag-background-color="red"
    tag-text-color="black"
></tag>

属性

属性 解释 类型
max-tag-number 允许最大的标签添加数量 Number|String
no-tag-rounded 去除标签的圆角效果 Boolean
tag-background-color 标签的背景颜色 String
tag-text-color 标签的文本颜色 String
border-color 边框颜色 String
border-radius 边框圆角 Number|String
tags 存储标签的数组,必须 Array

方法

方法 解释 类型
add-tag 添加tag的方法,参数为当前添加的tag Function
delete-tag 删除tag的方法,参数为当前tag所在数组的下标 Function
tip-text 自定义提示文本,若为函数,参数为允许的最大标签数 String|Fucntion
MIT License Copyright (c) 2022 Lucson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

一个可以动态增删的tag组件 支持丰富的自定义配置 展开 收起
README
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lucson/vue2-tag.git
git@gitee.com:lucson/vue2-tag.git
lucson
vue2-tag
vue2-tag
master

搜索帮助