Vue實(shí)現(xiàn)無限級(jí)樹形選擇器
前言:
想要在 Vue 中實(shí)現(xiàn)一個(gè)這樣的無限級(jí)樹形選擇器其實(shí)并不難,關(guān)鍵點(diǎn)在于利用 遞歸組件 和 高階事件監(jiān)聽,下面我們就一步步來實(shí)現(xiàn)它。
簡(jiǎn)單實(shí)現(xiàn)下樣式
創(chuàng)建 Tree.vue 組件(為方便閱讀,代碼有省略):
<template>
<ul class="treeMenu">
<li v-for="(item, index) in data" :key="index">
<i v-show="item.children" :class="triangle" />
<p :class="treeNode">
<label class="checkbox-wrap" @click="checked(item)">
<input v-if="isSelect" v-model="item.checked" type="checkbox" class="checkbox" />
</label>
<span class="title" @click="tap(item, index)">{{ item.title }}</span>
</p>
<!-- TODO -->
</li>
</ul>
</template>
<script>
export default {
name: 'TreeMenus',
props: {
data: {
type: Array,
default: () => [],
},
// 是否開啟節(jié)點(diǎn)可選擇
isSelect: {
type: Boolean,
default: false,
},
},
data() {
return {,
tapScopes: {},
scopes: {},
}
},
}
</script>
<style scoped>
...... some code ......
</style>展開收縮我們使用 CSS 來創(chuàng)建一個(gè)三角形:
.triangle {
width: 0;
height: 0;
border: 6px solid transparent;
border-left: 8px solid grey;
transition: all 0.1s;
left: 6px;
margin: 6px 0 0 0;
}然后定義一個(gè)展開時(shí)的 class,旋轉(zhuǎn)角度調(diào)整一下定位:
.caret-down {
transform: rotate(90deg);
left: 2px;
margin: 9px 0 0 0;
}
由于每個(gè)節(jié)點(diǎn)控制展開閉合的變量都是獨(dú)立的,為了不污染數(shù)據(jù),這里我們定義一個(gè)對(duì)象 tapScopes 來控制就好,記得使用 $set 來讓視圖響應(yīng)變化:
// 當(dāng)點(diǎn)擊三角形時(shí),圖標(biāo)變化:
changeStatus(index) {
this.$set(this.tapScopes, index, this.tapScopes[index] ? (this.tapScopes[index] === 'open' ? 'close' : 'open') : 'open')
}遞歸渲染
現(xiàn)在我們只渲染了第一層數(shù)據(jù),如何循環(huán)渲染下一級(jí)數(shù)據(jù)呢,其實(shí)很簡(jiǎn)單,往上面 TODO 的位置插入組件自身即可(相當(dāng)于引入了自身作為 components),只要組件設(shè)置了 name 屬性,
Vue 就可以調(diào)用該組件,:
<li v-for="(item, index) in data">
// .... some code ....
<tree-menus :data="item.children" v-bind="$props" />
</li>
<script>
export default {
name: 'TreeMenus'
// .... some code ....遞歸組件接收相同的 props 我們不必一個(gè)個(gè)傳遞,可以直接寫成 v-bind="$props" 把代理的 props 對(duì)象傳進(jìn)去(比如上面定義的 isSelect 就會(huì)被一直傳遞),只不過 data 被我們覆寫成了循環(huán)的下一級(jí)。最后使用 v-show 控制一下展開閉合的效果,基本的交互就實(shí)現(xiàn)出來了:
![]() | ![]() |
|---|
定義參數(shù)
樹形結(jié)構(gòu)數(shù)據(jù)一般都是如下的 嵌套結(jié)構(gòu),再?gòu)?fù)雜也只不過是字段變多了而已,這幾個(gè) 特征字段 是肯定存在的:key、label、children,以下面的參考數(shù)據(jù)為例: 這里的 key 是 id,用于標(biāo)識(shí)唯一性(該字段在整棵樹中是唯一的),label 則是 title 字段,用于顯示節(jié)點(diǎn)名稱,最后的 children 則是指下一級(jí)節(jié)點(diǎn),它的特征與父級(jí)一致。
[
{
id: 1,
title: "",
children: [{
id: 2,
title: "",
children: ......
}]
}
]所以我們的選擇器組件可以定義一個(gè)關(guān)鍵參數(shù)選項(xiàng),用于指定節(jié)點(diǎn)中的這幾個(gè)屬性值。
props: {
// ... some code ....
props: {
type: Object,
default: () => {
return {
children: 'children',
label: 'title',
key: 'id',
}
},
},
},組件中的一些關(guān)鍵參數(shù)都修改成動(dòng)態(tài)的形式:
:key="index" => :key="item[props.key]"
:data="item.children" => :data="item[props.children]"
{{ item.title }} => {{ item[props.label] }}實(shí)現(xiàn)點(diǎn)擊事件
現(xiàn)在我們來實(shí)現(xiàn)一個(gè)點(diǎn)擊事件 node-click: 為節(jié)點(diǎn)綁定一個(gè) click 事件,點(diǎn)擊后觸發(fā) $emit 把節(jié)點(diǎn)對(duì)象傳進(jìn)方法中即可:
<span class="title" @click="tap(item, index)"> ... </span>
methods: {
tap(item, index) {
this.$emit('node-click', item)
}
.........
// 調(diào)用時(shí):
<Tree @node-click="handle" :data="treeData" />
methods: {
handle(node) {
console.log('點(diǎn)擊節(jié)點(diǎn) Data : ', node)
}
.......這時(shí)問題來了,由于組件是遞歸嵌套的,如何在子節(jié)點(diǎn)中點(diǎn)擊時(shí)也能觸發(fā)最外層的事件呢?這時(shí)就需要利用 Vue 提供的 $listeners 這個(gè) property,配合 v-on="$listeners" 將所有的事件監(jiān)聽器指向組件中循環(huán)的子組件:
<tree-menus .... v-on="$listeners"></tree-menus>

往組件中定義任何其它方法,都可以像這樣正常觸發(fā)到調(diào)用它的組件那里。
完整代碼
Tree.vue
<template>
<ul class="treeMenu">
<li v-for="(item, index) in data" :key="item[props.key]">
<i v-show="item[props.children]" :class="['triangle', carets[tapScopes[index]]]" @click="changeStatus(index)" />
<p :class="['treeNode', { 'treeNode--select': item.onSelect }]">
<label class="checkbox-wrap" @click="checked(item)">
<input v-if="isSelect" v-model="item.checked" type="checkbox" class="checkbox" />
</label>
<span class="title" @click="tap(item, index)">{{ item[props.label] }}</span>
</p>
<tree-menus v-show="scopes[index]" :data="item[props.children]" v-bind="$props" v-on="$listeners"></tree-menus>
</li>
</ul>
</template>
<script>
const CARETS = { open: 'caret-down', close: 'caret-right' }
export default {
name: 'TreeMenus',
props: {
data: {
type: Array,
default: () => [],
},
isSelect: {
type: Boolean,
default: false,
},
props: {
type: Object,
default: () => {
return {
children: 'children',
label: 'title',
key: 'id',
}
},
},
},
data() {
return {
carets: CARETS,
tapScopes: {},
scopes: {},
}
},
methods: {
operation(type, treeNode) {
this.$emit('operation', { type, treeNode })
},
tap(item, index) {
this.$emit('node-click', item)
},
changeStatus(index) {
this.$emit('change', this.data[index])
// 圖標(biāo)變化
this.$set(this.tapScopes, index, this.tapScopes[index] ? (this.tapScopes[index] === 'open' ? 'close' : 'open') : 'open')
// 展開閉合
this.$set(this.scopes, index, this.scopes[index] ? false : true)
},
async checked(item) {
this.$emit('checked', item)
},
},
}
</script>
<style scoped>
.treeMenu {
padding-left: 20px;
list-style: none;
position: relative;
user-select: none;
}
.triangle {
transition: all 0.1s;
left: 6px;
margin: 6px 0 0 0;
position: absolute;
cursor: pointer;
width: 0;
height: 0;
border: 6px solid transparent;
border-left: 8px solid grey;
}
.caret-down {
transform: rotate(90deg);
left: 2px;
margin: 9px 0 0 0;
}
.checkbox-wrap {
display: flex;
align-items: center;
}
.checkbox {
margin-right: 0.5rem;
}
.treeNode:hover,
.treeNode:hover > .operation {
color: #3771e5;
background: #f0f7ff;
}
.treeNode--select {
background: #f0f7ff;
}
.treeNode:hover > .operation {
opacity: 1;
}
p {
position: relative;
display: flex;
align-items: center;
}
p > .title {
cursor: pointer;
}
a {
color: cornflowerblue;
}
.operation {
position: absolute;
right: 0;
font-size: 18px;
opacity: 0;
}
</style>Mock.js
export default {
stat: 1,
msg: 'ok',
data: {
list: [
{
key: 1,
title: '一級(jí)機(jī)構(gòu)部門',
children: [
{
key: 90001,
title: '測(cè)試機(jī)構(gòu)111',
children: [
{
key: 90019,
title: '測(cè)試機(jī)構(gòu)111-2',
},
{
key: 90025,
title: '機(jī)構(gòu)機(jī)構(gòu)',
children: [
{
key: 90026,
title: '機(jī)構(gòu)機(jī)構(gòu)-2',
},
],
},
],
},
{
key: 90037,
title: '另一個(gè)機(jī)構(gòu)部門',
},
],
},
{
key: 2,
title: '小賣部總舵',
children: [
{
key: 90037,
title: '小賣部河邊分部',
},
],
},
],
},
}調(diào)用組件:
<template>
<div class="about">
<Tree :isSelect="isSelect" :data="treeData" @node-click="handle" @change="loadData" />
</div>
</template>
<script>
import Tree from '@/Tree.vue'
import json from '@/mock.js'
export default {
components: { Tree },
data() {
return {
treeData: [],
isSelect: false,
defaultProps: {
children: 'children',
label: 'title',
key: 'id',
},
}
},
created() {
this.treeData = json.data.list
},
methods: {
handle(node) {
console.log('點(diǎn)擊節(jié)點(diǎn) Data : ', node)
},
loadData(treeNode) {
console.log(treeNode)
// eg: 動(dòng)態(tài)更新子節(jié)點(diǎn)
// treeNode.children = JSON.parse(JSON.stringify(json.data.list))
// this.treeData = [...this.treeData]
},
},
}
</script>到此這篇關(guān)于Vue實(shí)現(xiàn)無限級(jí)樹形選擇器的文章就介紹到這了,更多相關(guān)Vue選擇器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3+Element Plus實(shí)現(xiàn)自定義穿梭框的詳細(xì)代碼
找到一個(gè)好用的vue樹形穿梭框組件都很難,又不想僅僅因?yàn)橐粋€(gè)穿梭框在element-ui之外其他重量級(jí)插件,本文給大家分享vue3+Element Plus實(shí)現(xiàn)自定義穿梭框的示例代碼,感興趣的朋友一起看看吧2024-01-01
antd配置config-overrides.js文件的操作
這篇文章主要介紹了antd配置config-overrides.js文件的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10
Vue實(shí)現(xiàn)頁(yè)面添加滿屏水印和去除水印功能
在一些特殊的應(yīng)用場(chǎng)景中,可能需要在網(wǎng)頁(yè)上添加水印以保護(hù)版權(quán)或標(biāo)識(shí)信息,本文將介紹如何在Vue項(xiàng)目中添加滿屏水印并實(shí)現(xiàn)去除水印的功能,文中通過代碼示例講解的非常詳細(xì),需要的朋友可以參考下2024-07-07
Vue組件更新數(shù)據(jù)v-model不生效的解決
這篇文章主要介紹了Vue組件更新數(shù)據(jù)v-model不生效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
Vue?import?from省略后綴/加載文件夾的方法/實(shí)例詳解
本文介紹Vue在import時(shí)省略后綴以及import文件夾的方法,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
vue3中emit('update:modelValue')使用簡(jiǎn)單示例
這篇文章主要給大家介紹了關(guān)于vue3中emit('update:modelValue')使用的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-09-09



