小編給大家分享一下Vue怎么實(shí)現(xiàn)多標(biāo)簽選擇器,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

實(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" 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">
<!-- 待選標(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>
// 簡單封裝一個(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)聽條件變化,按照請求接口拼裝請求參數(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',
}]
}]以上是“Vue怎么實(shí)現(xiàn)多標(biāo)簽選擇器”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
當(dāng)前名稱:Vue怎么實(shí)現(xiàn)多標(biāo)簽選擇器-創(chuàng)新互聯(lián)
轉(zhuǎn)載源于:http://chinadenli.net/article10/dhopdo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、面包屑導(dǎo)航、網(wǎng)頁設(shè)計(jì)公司、網(wǎng)站設(shè)計(jì)、手機(jī)網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容