Vue實(shí)現(xiàn)多標(biāo)簽選擇器
本文實(shí)例為大家分享了Vue實(shí)現(xiàn)多標(biāo)簽選擇器展示的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)效果
實(shí)現(xiàn)代碼
<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" > --> <!-- <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"> <!-- 待選標(biāo)簽 --> <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> <!-- 標(biāo)簽 --> <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> <!-- 已選標(biāo)簽 --> <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> // 簡(jiǎn)單封裝一個(gè)公用組件 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, // 分類標(biāo)簽,可從外部加載配置 conditions: [] // 已選條件 } }, watch: { // 監(jiān)聽條件變化,按照請(qǐng)求接口拼裝請(qǐng)求參數(shù) 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: { // 處理標(biāo)簽點(diǎn)擊事件,未選中則選中,已選中則取消選中 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); } }, // 清除已選整個(gè)類別標(biāo)簽 clearCategory(category, categoryIndex) { category.count = 0; // 可選列表均為未選中狀態(tài) 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); } } }, // 移除一個(gè)條件 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>
標(biāo)簽篩選的數(shù)據(jù)格式
data.js
var categories = [{ name: '品牌', count: 0, children: [{ name: '聯(lián)想', }, { 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: '內(nèi)存', 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', }] }]
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ant-design表單處理和常用方法及自定義驗(yàn)證操作
這篇文章主要介紹了ant-design表單處理和常用方法及自定義驗(yàn)證操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10vue實(shí)現(xiàn)用戶長(zhǎng)時(shí)間不操作自動(dòng)退出登錄功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue實(shí)現(xiàn)用戶長(zhǎng)時(shí)間不操作自動(dòng)退出登錄功能的實(shí)現(xiàn)代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07vue發(fā)布到nginx下請(qǐng)求后臺(tái)404問題及解決
這篇文章主要介紹了vue發(fā)布到nginx下請(qǐng)求后臺(tái)404問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03vue3之Suspense加載異步數(shù)據(jù)的使用
本文主要介紹了vue3之Suspense加載異步數(shù)據(jù)的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02vue動(dòng)態(tài)的 BreadCrumb 組件el-breadcrumb ElementUI詳解
這篇文章主要介紹了vue如何做一個(gè)動(dòng)態(tài)的 BreadCrumb 組件,el-breadcrumb ElementUI2024-07-07
,本文通過圖文示例代碼相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下