基于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>
目录
相关文章
|
SQL 关系型数据库 MySQL
使用SQL语句创建数据库:一步步指南
使用SQL语句创建数据库:一步步指南
641 0
|
10月前
|
机器学习/深度学习 计算机视觉
YOLOv11改进策略【YOLO和Mamba】| 2024 VM-UNet,高效的特征提取模块VSS block 二次创新提高精度
YOLOv11改进策略【YOLO和Mamba】| 2024 VM-UNet,高效的特征提取模块VSS block 二次创新提高精度
1175 9
|
Kubernetes 负载均衡 安全
如何为Azure Kubernetes Services启用Internal Loadbalancer
执行上述步骤,您就可以搭建一个仅在Azure虚拟网络内部可用的内部负载均衡器了。这为内部服务通信提供了安全性,避免了外部世界的直接接入。与外部负载均衡器相比,内部负载均衡器通常用于企业内部或多层应用架构中的后端服务。通过仔细配置和管理AKS中的Service对象,您可以确保应用架构符合网络和安全性要求。
268 2
|
存储 消息中间件 缓存
apt安装Redis 7
【7月更文挑战第2天】
310 1
|
移动开发 安全 API
阿里云最新域名注册及续费和转入收费价格表参考
目前域名注册管理机构(Verisign)已上调.com中英文域名成本,这一变动将直接影响到全球范围内.com域名价格,各大注册商的.com域名注册、续费、转移价格已同步上涨。以阿里云为例,此次涨价之后,.com英文域名的注册价格由原来的78元涨价到了83元,续费价格也涨到了90元,下面是2024年9月1日涨价之后,阿里云最新的域名注册及续费和转入最新收费价格表。
|
定位技术
ArcGIS中ERROR 999999报错Configuration RasterCommander ImageServer can not be started解决
ArcGIS中ERROR 999999报错Configuration RasterCommander ImageServer can not be started解决
665 1
|
SQL 弹性计算 监控
海量数据下Lindorm查询实践
本教程将提供一台ECS 和云数据库Lindorm集群,带您体验通过 Lindorm SQL来查询信用卡客户的模拟数据,查看高并发请求下的,Lindorm响应时间的稳定性。
|
Python
Socket接口测试
Socket接口测试
436 0
|
人工智能 大数据 物联网
云计算
第一节云计算 1.云的概念 “云”是网络、互联网的一种比喻说法。通俗来讲,云是对互联网的升级,意味着互联网不仅用于存储数据,还可以为用户提供某种服务。 云”是指以云计算、网络及虚拟化为核心技术,通过一系列的硬件和软件,实现“按需服务”的一种计算机技术。 2.云计算的概念 云计算是一种通过Internet以服务的方式提供动态可伸缩的虚拟化资源的计算模式。 云计算是分布式计算、并行计算、效用计算、网络存储、虚拟化、负载均衡等传统计算机技术和网络技术发展结合的产物。 3.云计算的特点 虚拟化 高扩展性 按需服务 灵活性高 高可靠性 极其廉价 通用性强 超大规模 考点2 云计算
|
数据采集 Python
python爬取叮咚买菜评价数据
python爬取叮咚买菜评价数据