Vue實(shí)現(xiàn)無(wú)限級(jí)樹(shù)形選擇器
前言:
想要在 Vue 中實(shí)現(xiàn)一個(gè)這樣的無(wú)限級(jí)樹(shù)形選擇器其實(shí)并不難,關(guān)鍵點(diǎn)在于利用 遞歸組件 和 高階事件監(jiān)聽(tīng),下面我們就一步步來(lái)實(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: () => [], }, // 是否開(kāi)啟節(jié)點(diǎn)可選擇 isSelect: { type: Boolean, default: false, }, }, data() { return {, tapScopes: {}, scopes: {}, } }, } </script> <style scoped> ...... some code ...... </style>
展開(kāi)收縮我們使用 CSS 來(lái)創(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è)展開(kāi)時(shí)的 class
,旋轉(zhuǎn)角度調(diào)整一下定位:
.caret-down { transform: rotate(90deg); left: 2px; margin: 9px 0 0 0; }
由于每個(gè)節(jié)點(diǎn)控制展開(kāi)閉合的變量都是獨(dú)立的,為了不污染數(shù)據(jù),這里我們定義一個(gè)對(duì)象 tapScopes
來(lái)控制就好,記得使用 $set
來(lái)讓視圖響應(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ì)被一直傳遞),只不過(guò) data
被我們覆寫成了循環(huán)的下一級(jí)。最后使用 v-show
控制一下展開(kāi)閉合的效果,基本的交互就實(shí)現(xiàn)出來(lái)了:
![]() | ![]() |
---|
定義參數(shù)
樹(shù)形結(jié)構(gòu)數(shù)據(jù)一般都是如下的 嵌套結(jié)構(gòu),再?gòu)?fù)雜也只不過(guò)是字段變多了而已,這幾個(gè) 特征字段 是肯定存在的:key
、label
、children
,以下面的參考數(shù)據(jù)為例: 這里的 key 是 id
,用于標(biāo)識(shí)唯一性(該字段在整棵樹(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)在我們來(lái)實(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í)問(wèn)題來(lái)了,由于組件是遞歸嵌套的,如何在子節(jié)點(diǎn)中點(diǎn)擊時(shí)也能觸發(fā)最外層的事件呢?這時(shí)就需要利用 Vue 提供的 $listeners
這個(gè) property,配合 v-on="$listeners"
將所有的事件監(jiān)聽(tīng)器指向組件中循環(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') // 展開(kāi)閉合 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)無(wú)限級(jí)樹(shù)形選擇器的文章就介紹到這了,更多相關(guān)Vue選擇器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- ant-design-vue中的select選擇器,對(duì)輸入值的進(jìn)行篩選操作
- ant-design-vue 時(shí)間選擇器賦值默認(rèn)時(shí)間的操作
- ant design vue datepicker日期選擇器中文化操作
- 淺談Vue使用Cascader級(jí)聯(lián)選擇器數(shù)據(jù)回顯中的坑
- Vue?elementUI實(shí)現(xiàn)樹(shù)形結(jié)構(gòu)表格與懶加載
- Vue自定義驗(yàn)證之日期時(shí)間選擇器詳解
- vue樹(shù)形控件tree的使用方法
- Vue+Element樹(shù)形表格實(shí)現(xiàn)拖拽排序示例
相關(guān)文章
axios全局注冊(cè),設(shè)置token,以及全局設(shè)置url請(qǐng)求網(wǎng)段的方法
今天小編就為大家分享一篇axios全局注冊(cè),設(shè)置token,以及全局設(shè)置url請(qǐng)求網(wǎng)段的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09vue搜索頁(yè)開(kāi)發(fā)實(shí)例代碼詳解(熱門搜索,歷史搜索,淘寶接口演示)
這篇文章主要介紹了vue搜索頁(yè)開(kāi)發(fā)實(shí)例(熱門搜索,歷史搜索,淘寶接口演示),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04使用vue.js2.0 + ElementUI開(kāi)發(fā)后臺(tái)管理系統(tǒng)詳細(xì)教程(一)
這篇文章主要介紹了使用vue.js2.0 + ElementUI開(kāi)發(fā)后臺(tái)管理系統(tǒng)詳細(xì)教程(一)的相關(guān)資料,需要的朋友可以參考下2017-01-01vue cli使用融云實(shí)現(xiàn)聊天功能的實(shí)例代碼
最近小編接了一個(gè)新項(xiàng)目,項(xiàng)目需求要實(shí)現(xiàn)一個(gè)聊天功能,今天小編通過(guò)實(shí)例代碼給大家介紹vue cli使用融云實(shí)現(xiàn)聊天功能的方法,感興趣的朋友跟隨小編一起看看吧2019-04-04對(duì)vue事件的延遲執(zhí)行實(shí)例講解
今天小編就為大家分享一篇對(duì)vue事件的延遲執(zhí)行實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08解決vue組件銷毀之后計(jì)時(shí)器繼續(xù)執(zhí)行的問(wèn)題
這篇文章主要介紹了解決vue組件銷毀之后計(jì)時(shí)器繼續(xù)執(zhí)行的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07