element-plus的el-tree的雙向綁定示例代碼
el-tree改造了下
可選可取消 有默認(rèn)值 不包含父級(jí)id(也可打開(kāi)注釋 包含父級(jí)id) 默認(rèn)展開(kāi) 點(diǎn)擊節(jié)點(diǎn)也可觸發(fā)選擇 節(jié)點(diǎn)內(nèi)容自定義
<template> <div class="absolute"> <el-scrollbar class="pall"> <div class="ball mb radius overflow"> <div class="bb" style="background: rgba(124, 162, 121, 0.1); padding: 4px 16px;"> <el-checkbox @change="selectCheckBox($event,1)">拓?fù)鋱D樹(shù)結(jié)構(gòu)</el-checkbox> </div> <!-- default-checked-keys:默認(rèn)展開(kāi)值(正常來(lái)說(shuō)需要包含父級(jí)id的 但是我們后端不要后端id ) show-checkbox:多選框 node-key:每個(gè)樹(shù)節(jié)點(diǎn)用來(lái)作為唯一標(biāo)識(shí)的屬性,整棵樹(shù)應(yīng)該是唯一的 default-expand-all:是否默認(rèn)展開(kāi)所有節(jié)點(diǎn) expand-on-click-node:是否在點(diǎn)擊節(jié)點(diǎn)的時(shí)候展開(kāi)或者收縮節(jié)點(diǎn), 默認(rèn)值為 true,如果為 false,則只有點(diǎn)箭頭圖標(biāo)的時(shí)候才會(huì)展開(kāi)或者收縮節(jié)點(diǎn) default-checked-keys:默認(rèn)勾選的節(jié)點(diǎn)的 key 的數(shù)組 check-on-click-node:是否在點(diǎn)擊節(jié)點(diǎn)的時(shí)候選中節(jié)點(diǎn),默認(rèn)值為 false,即只有在點(diǎn)擊復(fù)選框時(shí)才會(huì)選中節(jié)點(diǎn) props:配置選項(xiàng),具體看下表 --> {{ childKeys }} <div style="padding: 4px 20px"> <el-tree :key="keyIndex" ref="treeRef" class="tree_ref" style="max-width: 600px" :data="data" show-checkbox node-key="id" :default-expand-all="true" :expand-on-click-node='false' :default-checked-keys="childKeys" :check-on-click-node="true" :props="defaultProps" @check-change="checkChange"> <template #default="{ node, data }"> <span class="custom-tree-node"> <span>{{ node.label }}</span> <span style="color:red;margin-left: 10px">id:{{data.id }}</span> </span> </template> </el-tree> </div> </div> </el-scrollbar> </div> </template> <script setup> import { ref } from 'vue'; let treeRef = ref(null); let childKeys = ref([5, 10]); // 初始化選中子節(jié)點(diǎn)的 ID const defaultProps = { children: 'children', label: 'label', }; let keyIndex = ref(1) // 注意該key在需要全id和只需要子集id的時(shí)候用法不同 const data = [ { id: 1, label: 'Level one 1', children: [ { id: 4, label: 'Level two 1-1', children: [ { id: 9, label: 'Level three 1-1-1', }, { id: 10, label: 'Level three 1-1-2', }, ], }, ], }, { id: 2, label: 'Level one 2', children: [ { id: 5, label: 'Level two 2-1', }, { id: 6, label: 'Level two 2-2', }, ], }, { id: 3, label: 'Level one 3', children: [ { id: 7, label: 'Level two 3-1', }, { id: 8, label: 'Level two 3-2', }, ], }, ]; const checkChange = () => { // 獲取所有選中的節(jié)點(diǎn)對(duì)象 const checkedNodes = treeRef.value.getCheckedNodes(); // // 結(jié)果1:獲取包含父節(jié)點(diǎn)的id // childKeys.value = treeRef.value.getCheckedKeys() // 結(jié)果2:提取子節(jié)點(diǎn)的 ID,不包括父節(jié)點(diǎn) childKeys.value = checkedNodes .filter(node => !node.children) // 只保留沒(méi)有子節(jié)點(diǎn)的節(jié)點(diǎn) .map(node => node.id); // 提取 ID console.log('默認(rèn)值', childKeys.value, checkedNodes); // 只包含子節(jié)點(diǎn)的 ID keyIndex.value++ } // 手動(dòng)全選 const selectCheckBox = (value, num) => { // console.log(value, num); // // 結(jié)果1:獲取包含父節(jié)點(diǎn)的id // if (value) { // childKeys.value = getAllNodeIds(data) // console.log('所有層級(jí)id', getAllNodeIds(data)); // } else { // // 取消勾選所有節(jié)點(diǎn) // childKeys.value = [] // treeRef.value.setCheckedKeys([]); // } // keyIndex.value++ // 結(jié)果2:提取子節(jié)點(diǎn)的 ID,不包括父節(jié)點(diǎn) if (value) { // 勾選所有節(jié)點(diǎn) const allKeys = data.flatMap(node => getAllNodeKeys(node)); treeRef.value.setCheckedKeys(allKeys); } else { childKeys.value = [] // 取消勾選所有節(jié)點(diǎn) treeRef.value.setCheckedKeys([]); } }; const getAllNodeKeys = (node) => { let keys = [node.id]; if (node.children) { node.children.forEach(child => { keys = keys.concat(getAllNodeKeys(child)); }); } console.log(keys); return keys; } // 遞歸函數(shù)獲取所有節(jié)點(diǎn)的 ID const getAllNodeIds = (nodes) => { let ids = []; nodes.forEach(node => { ids.push(node.id); // 添加當(dāng)前節(jié)點(diǎn)的 ID if (node.children) { ids = ids.concat(getAllNodeIds(node.children)); // 遞歸調(diào)用以獲取子節(jié)點(diǎn)的 ID } }); return ids; }; </script> <style lang="scss" scoped> :deep(.tree_ref) { margin-left: 12px; .el-tree-node__expand-icon { display: none; } } </style>
到此這篇關(guān)于element-plus的el-tree的雙向綁定的文章就介紹到這了,更多相關(guān)element-plus el-tree雙向綁定內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue使用Axios進(jìn)行跨域請(qǐng)求的方法詳解
在開(kāi)發(fā)現(xiàn)代?Web?應(yīng)用時(shí),前端和后端通常分離部署在不同的服務(wù)器上,這就會(huì)引發(fā)跨域請(qǐng)求問(wèn)題,所以本文將詳細(xì)介紹如何在?Vue?項(xiàng)目中使用?Axios?發(fā)起跨域請(qǐng)求時(shí)解決跨域問(wèn)題的相關(guān)資料,需要的朋友可以參考下2024-09-09vue音樂(lè)播放器插件vue-aplayer的配置及其使用實(shí)例詳解
本篇文章主要介紹了vue音樂(lè)播放器插件vue-aplayer的配置及其使用實(shí)例詳解,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07如何實(shí)現(xiàn)vue加載指令 v-loading
在日常的開(kāi)發(fā)中,加載效果是非常常見(jiàn)的,但是怎么才能方便的使用,本文介紹如何實(shí)現(xiàn)vue加載指令 v-loading,感興趣的朋友一起看看吧2024-01-01vue3中如何用threejs畫(huà)一些簡(jiǎn)單的幾何體
最近學(xué)習(xí)threejs有些時(shí)間了,就想著著手做些東西,下面這篇文章主要給大家介紹了關(guān)于vue3中如何用threejs畫(huà)一些簡(jiǎn)單的幾何體的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03Vue中動(dòng)畫(huà)與過(guò)渡的使用教程
最近在寫(xiě)vue的一個(gè)項(xiàng)目要實(shí)現(xiàn)過(guò)渡的效果,雖然vue動(dòng)畫(huà)不是強(qiáng)項(xiàng),庫(kù)也多,但是基本的坑還是得踩扎實(shí),下面這篇文章主要給大家介紹了關(guān)于Vue中實(shí)現(xiàn)過(guò)渡動(dòng)畫(huà)效果的相關(guān)資料,需要的朋友可以參考下2023-01-01利用vscode編寫(xiě)vue的簡(jiǎn)單配置詳解
這篇文章主要給大家介紹了利用vscode編寫(xiě)vue簡(jiǎn)單配置的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。2017-06-06vue實(shí)現(xiàn)滾動(dòng)條始終懸浮在頁(yè)面最下方
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)滾動(dòng)條始終懸浮在頁(yè)面最下方,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04