Vue3+elementPlus中 樹形控件封裝的實(shí)現(xiàn)
1.組件
<template>
<div class="selection">
<el-select placeholder="請(qǐng)選擇" v-model="nameList" clearable @clear="handleClear" ref="selectUpResId" style="width: 100%">
<el-option hidden :key="1" :value="1"></el-option
><!--這個(gè)必不可少否則顯示不出來下拉數(shù)據(jù)-->
<!-- check-strictly :父子是否聯(lián)動(dòng),根據(jù)業(yè)務(wù)修改 -->
<el-tree
:data="options"
node-key="id"
:props="defaultProps"
:default-checked-keys="huixianarr"
@check="handleNodeClick"
show-checkbox
ref="treeRef"
:check-strictly="true"
>
</el-tree>
</el-select>
</div>
</template>
<script setup name="selects">
import { ref, reactive } from "vue";
//接受父組件傳來的數(shù)據(jù)
const props = defineProps({
treeFilterData: {
type: Array,
default: () => [] //樹形控件數(shù)據(jù)源
},
treeHxlist: {
type: Array,
default: () => [] //回顯ID集合,根據(jù)業(yè)務(wù)修改
},
dfProps: {
type: Object,
default: () => {} //樹形控件配置項(xiàng),根據(jù)業(yè)務(wù)修改
}
});
const treeRef = ref();
let nameList = ref("");
let huixianarr = ref([]);
let idList = ref();
let options = ref([]);
let defaultProps = ref({});
defaultProps.value = props.dfProps;
let hxlist = ref([]);
let treeForm = ref();
let list = ref();
var propertyName = props.dfProps.label;
init();
function init() {
options.value = props.treeFilterData;
huixianarr.value = props.treeHxlist;
let hxlist = findPathsByIds(options.value, huixianarr.value);
nameList.value = hxlist.join(","); //顯示內(nèi)容
}
const emit = defineEmits(["checKedId"]);
function handleNodeClick(data, lst) {
let arr = [],
name = [];
lst.checkedNodes.forEach(item => {
//過濾拿到選中的id
arr.push(item.id);
});
lst.checkedNodes.forEach(item => {
//過濾拿到選中的name
name.push(item[propertyName]);
});
nameList.value = name.join(","); //顯示內(nèi)容
idList.value = arr; //后臺(tái)傳參需要的id
//傳給父組件
emit("checKedId", idList.value);
}
function handleClear() {
hxlist.value = [];
idList.value = []; //id集合
nameList.value = ""; //input顯示內(nèi)容
huixianarr.value = []; //回顯ID集合
treeRef.value.setCheckedKeys([]); //清空
}
function findPathsByIds(data, targetIds) {
const resultPaths = []; // 存儲(chǔ)匹配的 title
// 輔助函數(shù):遞歸查找單個(gè) id 的 title
function findPathRecursive(items, targetId) {
for (const item of items) {
// 如果當(dāng)前項(xiàng)的 id 匹配,添加其 title 到結(jié)果數(shù)組
if (item.id === targetId) {
resultPaths.push(item[propertyName]);
return; // 找到后直接返回
}
// 如果有 children,遞歸查找
if (item.children && item.children.length > 0) {
findPathRecursive(item.children, targetId);
}
}
}
// 遍歷目標(biāo) id 數(shù)組,逐一查找
for (const id of targetIds) {
findPathRecursive(data, id);
}
return resultPaths;
}
</script>
<style scoped>
.selection {
width: 300px;
}
</style>
2.使用
<Selectoption :treeFilterData="treeFilterData" :treeHxlist="treeHxlist" :dfProps="dfProps" @checKedId="gettreelist" />
//回顯
const treeFilterData = ref([1]);
//格式
let dfProps = ref({
children: "children",
label: "title"
});
//數(shù)據(jù)
const treeFilterData = ref([
{
"id": 1,
"path": "/home/index",
"name": "home",
"component": "/home/index",
"title": "首頁",
"meta": {
"icon": "HomeFilled",
"title": "首頁",
"isLink": "",
"isHide": false,
"isFull": false,
"isAffix": true,
"isKeepAlive": true
}
},
{
"id": 6,
"path": "/system",
"name": "system",
"redirect": "/system/accountManage",
"title": "系統(tǒng)管理",
"meta": {
"icon": "Tools",
"title": "系統(tǒng)管理",
"isLink": "",
"isHide": false,
"isFull": false,
"isAffix": false,
"isKeepAlive": true
},
"children": [
{
"id": 61,
"father": 6,
"path": "/system/accountManage",
"name": "accountManage",
"component": "/system/accountManage/index",
"title": "賬號(hào)管理",
"meta": {
"icon": "Menu",
"title": "賬號(hào)管理",
"isLink": "",
"isHide": false,
"isFull": false,
"isAffix": false,
"isKeepAlive": true
}
},
]
}
]);
到此這篇關(guān)于Vue3+elementPlus中 樹形控件封裝的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Vue3 elementPlus樹形控件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue(element ui)el-table樹形表格展示以及數(shù)據(jù)對(duì)齊方式
- ElementUI?el-table?樹形數(shù)據(jù)的懶加載的實(shí)現(xiàn)
- elementUI Tree 樹形控件單選實(shí)現(xiàn)示例
- Element-UI控件Tree實(shí)現(xiàn)數(shù)據(jù)樹形結(jié)構(gòu)的方法
- Vue Element UI 中 el-table 樹形數(shù)據(jù) tree-props 多層級(jí)使用避坑指南
- Element控件Tree實(shí)現(xiàn)數(shù)據(jù)樹形結(jié)構(gòu)的示例代碼
- vue2+elementUI實(shí)現(xiàn)下拉樹形多選框效果實(shí)例
- Vue+Element樹形表格實(shí)現(xiàn)拖拽排序示例
相關(guān)文章
詳解vue如何使用rules對(duì)表單字段進(jìn)行校驗(yàn)
這篇文章主要介紹了詳解vue如何使用rules對(duì)表單字段進(jìn)行校驗(yàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10
在Vue中使用axios請(qǐng)求攔截的實(shí)現(xiàn)方法
這篇文章主要介紹了在Vue中使用axios請(qǐng)求攔截,需要的朋友可以參考下2018-10-10
詳解Vue中l(wèi)ocalstorage和sessionstorage的使用
這篇文章主要介紹了詳解Vue中l(wèi)ocalstorage和sessionstorage的使用方法和經(jīng)驗(yàn)心得,有需要的朋友跟著小編參考學(xué)習(xí)下吧。2017-12-12
element中el-table局部刷新的實(shí)現(xiàn)示例
本文主要介紹了element中el-table局部刷新的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
vue3+vite使用History路由模式打包部署項(xiàng)目的步驟及注意事項(xiàng)
這篇文章主要介紹了vue3+vite使用History路由模式打包部署項(xiàng)目的步驟及注意事項(xiàng),配置過程包括在Vue項(xiàng)目中設(shè)置路由模式、調(diào)整打包配置以及Nginx服務(wù)器的配置,正確的部署配置能夠確保應(yīng)用順利運(yùn)行,提升用戶體驗(yàn),需要的朋友可以參考下2024-10-10
npm install -g @vue/cli安裝vue腳手架報(bào)錯(cuò)問題(一般都能解決)
這篇文章主要介紹了npm install -g @vue/cli安裝vue腳手架報(bào)錯(cuò)問題(一般都能解決),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
Vue element商品列表的增刪改功能實(shí)現(xiàn)
這篇文章主要介紹了Vue+element 商品列表、新增、編輯、刪除業(yè)務(wù)實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
基于vue實(shí)現(xiàn)新聞自下往上滾動(dòng)效果(示例代碼)
這篇文章主要介紹了vue新聞自下往上滾動(dòng)效果,當(dāng)鼠標(biāo)鼠標(biāo)放上暫停滾動(dòng),鼠標(biāo)移出繼續(xù)滾動(dòng),本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
vue+springboot實(shí)現(xiàn)項(xiàng)目的CORS跨域請(qǐng)求
這篇文章主要介紹了vue+springboot實(shí)現(xiàn)項(xiàng)目的CORS跨域請(qǐng)求,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09
vue跳轉(zhuǎn)同一個(gè)組件,參數(shù)不同,頁面接收值只接收一次的解決方法
今天小編就為大家分享一篇vue跳轉(zhuǎn)同一個(gè)組件,參數(shù)不同,頁面接收值只接收一次的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11

