如何在?Vue3?項(xiàng)目中使用?el-tree
el-tree 是 Element Plus 提供的樹形控件,用于展示具有層級(jí)關(guān)系的數(shù)據(jù),如組織架構(gòu)、文件目錄、分類菜單等。
在 Vue3 項(xiàng)目中使用 el-tree
一、基礎(chǔ)用法
1. 引入組件
<template>
<el-tree
:data="treeData"
:props="defaultProps"
@node-click="handleNodeClick"
/>
</template>
<script setup>
// 樹形數(shù)據(jù)
const treeData = [
{
label: '一級(jí)節(jié)點(diǎn) 1',
children: [
{
label: '二級(jí)節(jié)點(diǎn) 1-1',
children: [
{ label: '三級(jí)節(jié)點(diǎn) 1-1-1' }
]
}
]
},
{
label: '一級(jí)節(jié)點(diǎn) 2'
}
]
// 配置項(xiàng)(映射數(shù)據(jù)中的字段)
const defaultProps = {
children: 'children', // 子節(jié)點(diǎn)字段名
label: 'label' // 顯示文本的字段名
}
// 節(jié)點(diǎn)點(diǎn)擊事件
const handleNodeClick = (data, node, event) => {
console.log('點(diǎn)擊節(jié)點(diǎn):', data, node, event)
}
</script>二、常用功能與配置
1. 節(jié)點(diǎn)選擇(復(fù)選框 / 單選)
- 復(fù)選框:添加
show-checkbox屬性,支持多選 - 單選:結(jié)合
check-strictly(父子不關(guān)聯(lián))和check-on-click-node(點(diǎn)擊節(jié)點(diǎn)選中)
<el-tree
:data="treeData"
:props="defaultProps"
show-checkbox <!-- 顯示復(fù)選框 -->
check-strictly <!-- 父子節(jié)點(diǎn)不關(guān)聯(lián) -->
check-on-click-node <!-- 點(diǎn)擊節(jié)點(diǎn)即可選中 -->
@check-change="handleCheckChange" <!-- 選中狀態(tài)變化事件 -->
/>
<script setup>
// 選中狀態(tài)變化事件
const handleCheckChange = (data, checked, indeterminate) => {
console.log('選中變化:', data, checked, indeterminate)
}
</script>2. 展開 / 折疊控制
default-expand-all:默認(rèn)展開所有節(jié)點(diǎn)expand-on-click-node:點(diǎn)擊節(jié)點(diǎn)時(shí)展開 / 折疊(默認(rèn)true):default-expanded-keys:默認(rèn)展開指定節(jié)點(diǎn)(通過節(jié)點(diǎn)id控制)
<el-tree :data="treeData" :props="defaultProps" :default-expanded-keys="[1, 3]" <!-- 默認(rèn)展開 id 為 1 和 3 的節(jié)點(diǎn) --> :expand-on-click-node="false" <!-- 點(diǎn)擊節(jié)點(diǎn)不展開,僅通過箭頭控制 --> />
3. 自定義節(jié)點(diǎn)內(nèi)容
通過 scoped-slot 自定義節(jié)點(diǎn)顯示內(nèi)容(如添加圖標(biāo)、按鈕等):
<el-tree :data="treeData" :props="defaultProps">
<!-- 自定義節(jié)點(diǎn)內(nèi)容 -->
<template #default="{ node, data }">
<div class="custom-node">
<el-icon v-if="data.children"><Folder /></el-icon>
<el-icon v-else><Document /></el-icon>
<span>{{ node.label }}</span>
<el-button size="mini" @click.stop="handleEdit(data)">編輯</el-button>
</div>
</template>
</el-tree>
<script setup>
import { Folder, Document } from '@element-plus/icons-vue'
const handleEdit = (data) => {
console.log('編輯節(jié)點(diǎn):', data)
}
</script>4. 搜索過濾節(jié)點(diǎn)
結(jié)合 filter-method 實(shí)現(xiàn)節(jié)點(diǎn)搜索功能:
<template>
<el-input
v-model="filterText"
placeholder="輸入關(guān)鍵詞搜索"
style="margin-bottom: 10px"
/>
<el-tree
:data="treeData"
:props="defaultProps"
:filter-method="filterNode"
ref="treeRef"
/>
</template>
<script setup>
import { ref, watch } from 'vue'
const filterText = ref('')
const treeRef = ref(null)
// 過濾方法
const filterNode = (value, data) => {
if (!value) return true
// 匹配節(jié)點(diǎn)文本(支持模糊搜索)
return data.label.toLowerCase().includes(value.toLowerCase())
}
// 監(jiān)聽搜索文本變化,觸發(fā)過濾
watch(filterText, (val) => {
treeRef.value.filter(val)
})
</script>5. 獲取選中節(jié)點(diǎn)
通過組件的 getCheckedNodes 方法獲取選中的節(jié)點(diǎn)(復(fù)選框模式):
<template>
<el-button @click="getChecked">獲取選中節(jié)點(diǎn)</el-button>
<el-tree
ref="treeRef"
:data="treeData"
:props="defaultProps"
show-checkbox
/>
</template>
<script setup>
import { ref } from 'vue'
const treeRef = ref(null)
const getChecked = () => {
// 獲取所有選中節(jié)點(diǎn)(參數(shù)為 true 時(shí)僅返回葉子節(jié)點(diǎn))
const checkedNodes = treeRef.value.getCheckedNodes(false, false)
console.log('選中節(jié)點(diǎn):', checkedNodes)
}
</script>三、動(dòng)態(tài)加載節(jié)點(diǎn)
對于大數(shù)據(jù)場景,可通過 load 事件實(shí)現(xiàn)節(jié)點(diǎn)的懶加載(按需加載子節(jié)點(diǎn)):
<el-tree
:data="treeData"
:props="defaultProps"
lazy <!-- 啟用懶加載 -->
:load="loadNode" <!-- 加載子節(jié)點(diǎn)的方法 -->
ref="treeRef"
/>
<script setup>
const loadNode = (node, resolve) => {
// 根節(jié)點(diǎn)(level 為 0)初始加載
if (node.level === 0) {
return resolve([{ label: '初始節(jié)點(diǎn)', id: 1 }])
}
// 加載子節(jié)點(diǎn)(模擬異步請求)
setTimeout(() => {
const childNodes = [
{ label: `子節(jié)點(diǎn) ${node.data.id}-1`, id: `${node.data.id}-1` },
{ label: `子節(jié)點(diǎn) ${node.data.id}-2`, id: `${node.data.id}-2` }
]
resolve(childNodes)
}, 500)
}
</script>到此這篇關(guān)于在 Vue3 項(xiàng)目中使用 el-tree的文章就介紹到這了,更多相關(guān)vue3 el-tree使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- java+vue3+el-tree實(shí)現(xiàn)樹形結(jié)構(gòu)操作代碼
- vue3 element-plus el-tree自定義圖標(biāo)方式
- 在vue3中使用el-tree-select實(shí)現(xiàn)樹形下拉選擇器效果
- vue3中使用vuedraggable實(shí)現(xiàn)拖拽el-tree數(shù)據(jù)分組功能
- Vue3中無法為el-tree-select設(shè)置反選問題解析
- Vue+Elementui el-tree樹只能選擇子節(jié)點(diǎn)并且支持檢索功能
- vue中el-select 和el-tree二次封裝實(shí)現(xiàn)
相關(guān)文章
vue-router2.0 組件之間傳參及獲取動(dòng)態(tài)參數(shù)的方法
下面小編就為大家?guī)硪黄獀ue-router2.0 組件之間傳參及獲取動(dòng)態(tài)參數(shù)的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11
Vue+EleMentUI實(shí)現(xiàn)el-table-colum表格select下拉框可編輯功能實(shí)例
這篇文章主要給大家介紹了關(guān)于Vue+EleMentUI實(shí)現(xiàn)el-table-colum表格select下拉框可編輯功能的相關(guān)資料,element-UI表格的使用相信大家都不陌生,文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下2023-07-07
Vue+ElementUi實(shí)現(xiàn)點(diǎn)擊表格鏈接頁面跳轉(zhuǎn)和路由效果
這篇文章主要介紹了Vue+ElementUi實(shí)現(xiàn)點(diǎn)擊表格中鏈接進(jìn)行頁面跳轉(zhuǎn)和路由,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-03-03
Vue前端如何實(shí)現(xiàn)生成PDF并下載功能詳解
在前端的崗位上經(jīng)常需要實(shí)現(xiàn)個(gè)生成個(gè)并下載的可視化圖表頁P(yáng)DF文件,這篇文章主要給大家介紹了關(guān)于Vue前端如何實(shí)現(xiàn)生成PDF并下載功能的相關(guān)資料,需要的朋友可以參考下2021-10-10
基于Vue實(shí)現(xiàn)Excel解析與導(dǎo)出功能詳解
導(dǎo)入導(dǎo)出excel這是前端做管理系統(tǒng)最常用的功能了,下面這篇文章主要給大家介紹了基于Vue實(shí)現(xiàn)Excel解析與導(dǎo)出功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-08-08

