基于Vue实现多标签选择器

简介: 基于Vue实现多标签选择器

实现效果

d24.2.png


实现代码

<html lang="en">

<head>
    <title>Document</title>

    <!-- 引入本地组件库 -->
    <link rel="stylesheet" href="static/element-ui/index.css">
    <script src="static/element-ui/vue.js"></script>
    <script src="static/element-ui/index.js"></script>

    <!-- 引入CDN样式 -->
    <!-- <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css"> -->
    <!-- <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> -->
    <!-- <script src="https://unpkg.com/element-ui/lib/index.js"></script> -->

    <style>
        .not-active {
            display: inline-block;
            font-size: 12px;
            margin: 5px 8px;
        }
        
        span {
            margin: 0 2px;
        }
    </style>
</head>

<body>

    <div id="app">
        <!-- 待选标签 -->
        <div v-for='(category, categoryIndex) in categories' :key="category.id">
            <!-- 分类  -->
            <span class="not-active">{
        {category.name}}:</span>

            <template>
                <span  v-if="category.count"class="not-active" @click="clearCategory(category, categoryIndex)"> 不限</span>
                <my-tag v-else>不限</my-tag>
            </template>

            <!-- 标签  -->
            <template v-for='(child, childIndex) in category.children'>
                <my-tag  v-if="child.active" :closable='true'  @click-child='clickChild(category, categoryIndex, child, childIndex)'>
                    {
        { child.name }}
                </my-tag>
                <span v-else class="not-active" @click='clickChild(category, categoryIndex, child, childIndex)'>{
        { child.name }}</span>
            </template>
        </div>

        <!-- 已选标签 -->
        <div v-if='conditions.length'>
            <span class="not-active" @click="clearCondition">清空已选:<span>
                
            <el-tag
            v-for='(condition, index) in conditions' 
            :key="condition.id"
            type="primary"
            :closable="true"
            size="small"
            :disable-transitions="true"
            @close='removeCondition(condition, index)'
            @click='removeCondition(condition, index)'>
                {
        {condition.name}}
            </el-tag>
        </div>
    </div>

    <script src="./data.js"></script>

    <script>
        // 简单封装一个公用组件
        Vue.component('my-tag', {
            template: "<el-tag v-bind='$attrs' v-on='$listeners' effect='dark' size='small' :disable-transitions='true' @click='clickChild' @close='clickChild'><slot></slot></el-tag>",

            methods: {
                clickChild() {
                    this.$emit("click-child")
                }
            }
        });

        var app = new Vue({
            el: '#app',
            data() {
                return {
                    categories, // 分类标签,可从外部加载配置
                    conditions: [] // 已选条件
                }
            },

            watch: {
                // 监听条件变化,按照请求接口拼装请求参数
                conditions(val){
                    let selectedCondition = {};

                    for(let categorie of this.categories){
                        let selected_list = [];
                        for(let child of categorie.children){
                            if(child.active){
                                selected_list.push(child.name);
                            }
                        }
                        selectedCondition[categorie.name] = selected_list.join("|")
                    }
                    console.log(selectedCondition);
                }
            },

            methods: {
                // 处理标签点击事件,未选中则选中,已选中则取消选中
                clickChild(category, categoryIndex, child, childIndex) {
                    let uid = `${categoryIndex}-${childIndex}`
                    child.uid = uid;
                    console.log(uid)
                    
                    // 取消选择
                    if (child.active === true) {
                        category.count--;
                        child.active = false;
                        this.conditions.forEach((conditionChild, index) => {
                            if (conditionChild.uid === child.uid) {
                                this.conditions.splice(index, 1);
                            }
                        });
                    // 选择
                    } else {
                        category.count++;
                        child.active = true;
                        this.conditions.push(child);
                    }
                },
            
                // 清除已选整个类别标签
                clearCategory(category, categoryIndex) {
                    category.count = 0;
                    
                    // 可选列表均为未选中状态
                    category.children.forEach(child => {
                        child.active = false;
                    })

                    // 清空该类已选元素
                    for (let index = this.conditions.length - 1; index >= 0; index--) {
                        const conditionChild = this.conditions[index];
                        if (conditionChild.uid.startsWith(categoryIndex)) {
                            this.conditions.splice(index, 1);
                        }
                    }
                },
                
                // 移除一个条件
                removeCondition(condition, index) {
                    let categoryIndex = condition.uid.split("-")[0];
                    this.categories[categoryIndex].count --;

                    this.conditions.splice(index, 1)
                    condition.active = false;
                },

                // 清空所有条件
                clearCondition() {
                    for(let i = this.conditions.length-1; i >=0 ; i--){
                        this.removeCondition(this.conditions[i], i);
                    }
                }
            }
        });
    </script>

</body>

</html>

标签筛选的数据格式

data.js

var categories = [{

name: '品牌',
count: 0,
children: [{
name: '联想',
}, {
name: '小米',

}, {
name: '苹果',

}, {
name: '东芝',

}]
}, {
name: 'CPU',
count: 0,
children: [{
name: 'intel i7 8700K',

}, {
name: 'intel i7 7700K',

}, {
name: 'intel i9 9270K',

}, {
name: 'intel i7 8700',

}, {
name: 'AMD 1600X',


}]
}, {
name: '内存',
count: 0,
children: [{
name: '七彩虹8G',

}, {
name: '七彩虹16G',

}, {
name: '金士顿8G',

}, {
name: '金士顿16G',

}]
}, {
name: '显卡',
count: 0,
children: [{
name: 'NVIDIA 1060 8G',

}, {
name: 'NVIDIA 1080Ti 16G',

}, {
name: 'NVIDIA 1080 8G',

}, {
name: 'NVIDIA 1060Ti 16G',

}]
}]


            </div>
目录
相关文章
xal
|
JavaScript API
VSCode插件开发全攻略(九)常用API总结
更多文章请戳[VSCode插件开发全攻略系列目录导航](https://www.atatech.org/articles/121864)。 本文提炼一些常见的API使用场景供参考。 # 本文还没写完,有待补充 # 编辑器相关 ## 修改当前激活编辑器内容 替换当前编辑器全部内容: ```js vscode.window.activeTextEditor.e
xal
2970 0
|
编译器 C语言 C++
C/C++内存对齐规则(结构体、联合体、类)
C/C++内存对齐规则(结构体、联合体、类)
|
4月前
|
监控 数据可视化 定位技术
如何开发一套绩效管理(OKR)系统?(附架构图+流程图+代码参考)
本文详细介绍如何构建一套高效的OKR绩效管理系统,涵盖系统功能模块、业务流程、开发技巧与实现效果,助力企业提升目标管理与员工绩效。
|
5月前
|
数据可视化 调度 图形学
《质光相济:Three.js中3D视觉的底层交互逻辑》
本文深入探讨了Three.js构建3D场景时,光照与材质的深层交互逻辑。解析了平行光、点光源、聚光灯等不同光源的物理隐喻及其对场景氛围的影响,揭示了材质通过反射、吸收、透射等方式响应光线的本质。阐述了光照与材质参数组合产生的视觉化学反应,以及这种互动在塑造3D场景真实感与叙事性中的关键作用。强调开发者需从现实观察中提炼光影规律,突破参数调优表层认知,以构建兼具技术精度与美学深度的虚拟空间。
170 0
|
9月前
|
安全 Unix Linux
VMware Workstation 17.6.3 发布下载,现在完全免费无论个人还是商业用途
VMware Workstation 17.6.3 发布下载,现在完全免费无论个人还是商业用途
82462 65
|
开发框架 前端开发 JavaScript
《Vue3实战》用路由实现跳转登录、退出登录以及路由全局守护
《Vue3实战》用路由实现跳转登录、退出登录以及路由全局守护
|
机器学习/深度学习 算法
算法人生(2):从“强化学习”看如何“活在当下”
本文探讨了强化学习的原理及其在个人生活中的启示。强化学习强调智能体在动态环境中通过与环境交互学习最优策略,不断迭代优化。这种思想类似于“活在当下”的哲学,要求人们专注于当前状态和决策,不过分依赖历史经验或担忧未来。活在当下意味着全情投入每一刻,不被过去或未来牵绊。通过减少执着,提高觉察力和静心练习,我们可以更好地活在当下,同时兼顾历史经验和未来规划。文章建议实践静心、时间管理和接纳每个瞬间,以实现更低焦虑、更高生活质量的生活艺术。
165 2
|
SQL 存储 开发工具
vanna+qwen实现私有模型的SQL转换
本文档介绍了如何在本地部署Vanna服务以使用Qwen模型进行text2sql转换。首先,通过`snapshot_download`下载Qwen-7B-Chat模型,并安装相关依赖。接着,修改`openai_api.py`设置本地LLM服务接口。然后,安装并配置Vanna Flask服务,包括自定义LLM服务、连接数据库以及修改端口。为了解决内网访问问题,使用ngrok或natapp进行内网穿透,提供公网访问。最后,处理了chromadb包中自动下载资源的问题,以防网络不佳导致的失败。通过这些步骤,实现了使用本地Qwen模型的Vanna服务。
11294 9
|
存储 缓存 算法
高并发架构设计三大利器:缓存、限流和降级
软件系统有三个追求:高性能、高并发、高可用,俗称三高。本篇讨论高并发,从高并发是什么到高并发应对的策略、缓存、限流、降级等。
4009 6