在vue3中使用el-tree-select實(shí)現(xiàn)樹(shù)形下拉選擇器效果
el-tree-select是一個(gè)含有下拉菜單的樹(shù)形選擇器,結(jié)合了 el-tree 和 el-select 兩個(gè)組件的功能。
因?yàn)榘薳l-tree的功能,我們可以自定義tree的節(jié)點(diǎn),創(chuàng)造出想要的組件
使用default插槽可以自定義節(jié)點(diǎn)內(nèi)容,它的default插槽相當(dāng)于el-tree的default插槽
<template> <el-tree-select v-model="dirCode" :data="treeData" :highlight-current="true" :props="defaultProps" clearable filterable node-key="pathCode" :placeholder="placeholder" @clear="handleClear"> <template #default="{ node, data }"> <div class="custom-tree-node" @click="data.pathCode !== '-1' ? handleNodeClick(data) : ''"> <div class="tree-icon"> <!-- 這里的svg-icon是我自己加的,可以改成element-plus中的icon ----> <svg-icon class="file" icon-class="file"></svg-icon> </div> <div class="tree-label one-line"> <span class="tree-label-text one-line"> {{ node.label }} </span> </div> </div> </template> </el-tree-select> </template>
使用:model-value="modelValue"可以在適合用組件時(shí)直接v-model綁定值
我這里使用的是setup式的語(yǔ)法,當(dāng)然也可以使用setup()方法
<script setup> import { ref, reactive, watch, onMounted } from 'vue' import { getDirectory } from 'api/autoOperations/scriptManage' const props = defineProps({ placeholder: { type: String, default: '請(qǐng)選擇目錄', required: false }, code: { type: String, default: '', required: false }, path: { type: String, default: '', required: false } }) let dirCode = ref('') let dirPath = ref('') const treeData = ref([]) const emits = defineEmits(['change']) // 樹(shù)狀圖默認(rèn)配置 const defaultProps = reactive({ children: 'children', label: 'pathName', isLeaf(data, node) { return data.isLeaf == 'true' } }) watch(() => props.code, (val) => { if (val) { dirCode.value = val } }, { immediate: true, deep: true }) watch(() => props.path, (val) => { if (val) { dirPath.value = val } }, { immediate: true, deep: true }) onMounted(() => { getTreeData() }) // 這里從數(shù)據(jù)庫(kù)獲取數(shù)據(jù) const getTreeData = () => { } const handleNodeClick = (data) => { dirCode.value = data.pathCode dirPath.value = data.dirPath emits('change', { dirPath: dirPath.value, dirCode: dirCode.value }) } const handleClear = () => { dirCode.value = '' dirPath.value = '' emits('change', { dirPath: dirPath.value, dirCode: dirCode.value }) } </script>
這是我的自定義樣式,用的scss
<style lang="scss" scoped> .custom-tree-node { display: flex; justify-content: space-between; align-items: center; width: calc(100% - 24px); font-size: 12px; line-height: 24px; .tree-icon { width: 20px; display: flex; align-items: center; .file { width: 20px; font-size: 20px; vertical-align: text-bottom; } } .tree-label { width: 100%; height: 24px; line-height: 24px; .tree-label-text { display: inline-block; max-width: calc(100% - 30px); } } } </style>
最后是效果圖
到此這篇關(guān)于在vue3中使用el-tree-select做一個(gè)樹(shù)形下拉選擇器的文章就介紹到這了,更多相關(guān)vue3樹(shù)形下拉選擇器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue2+elementUI的el-tree的懶加載功能
- vue2+elementUI的el-tree的選中、高亮、定位功能的實(shí)現(xiàn)
- java+vue3+el-tree實(shí)現(xiàn)樹(shù)形結(jié)構(gòu)操作代碼
- Vue中el-tree樹(shù)全部展開(kāi)或收起的實(shí)現(xiàn)示例
- vue之el-tree懶加載數(shù)據(jù)并且實(shí)現(xiàn)樹(shù)的過(guò)濾問(wèn)題
- vue el-select與el-tree實(shí)現(xiàn)支持可搜索樹(shù)型
- Vue+Elementui el-tree樹(shù)只能選擇子節(jié)點(diǎn)并且支持檢索功能
相關(guān)文章
vue之?dāng)?shù)據(jù)交互實(shí)例代碼
本篇文章主要介紹了vue之?dāng)?shù)據(jù)交互實(shí)例代碼,vue中也存在像ajax和jsonp的數(shù)據(jù)交互,實(shí)現(xiàn)向服務(wù)器獲取數(shù)據(jù),有興趣的可以了解一下2017-06-06vue過(guò)濾器實(shí)現(xiàn)日期格式化的案例分析
這篇文章主要介紹了vue過(guò)濾器實(shí)現(xiàn)日期格式化的案例分析,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07vue使用swiper插件實(shí)現(xiàn)輪播圖的示例
這篇文章主要介紹了vue使用swiper插件實(shí)現(xiàn)輪播圖的示例,幫助大家更好的理解和學(xué)習(xí)使用vue框架,感興趣的朋友可以了解下2021-05-05vue插件vue-resource的使用筆記(小結(jié))
本篇文章主要介紹了vue插件vue-resource的使用筆記(小結(jié)),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08Vue3使用vue-office插件實(shí)現(xiàn)word預(yù)覽功能
vue-office是一個(gè)支持多種文件(docx、.xlsx、pdf)預(yù)覽的vue組件庫(kù),支持vue2和vue3,這篇文章主要介紹了Vue3使用vue-office插件實(shí)現(xiàn)word預(yù)覽功能,需要的朋友可以參考下2024-04-04基于Vue實(shí)現(xiàn)鼠標(biāo)滾動(dòng)輪控制頁(yè)面橫向滑動(dòng)效果
這篇文章主要介紹了如何基于Vue實(shí)現(xiàn)鼠標(biāo)滾動(dòng)輪控制頁(yè)面橫向滑動(dòng)效果,文中通過(guò)代碼示例和圖文結(jié)合的方式給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-09-09vue3+vite+SQL.js如何讀取db3文件數(shù)據(jù)
這篇文章主要介紹了vue3+vite+SQL.js如何讀取db3文件數(shù)據(jù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05Vue項(xiàng)目中首頁(yè)長(zhǎng)時(shí)間白屏的原因以及解決過(guò)程
這篇文章主要介紹了Vue項(xiàng)目中首頁(yè)長(zhǎng)時(shí)間白屏的原因以及解決過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01