uni-app無(wú)限級(jí)樹(shù)形組件簡(jiǎn)單實(shí)現(xiàn)代碼
因?yàn)轫?xiàng)目一些數(shù)據(jù)需要樹(shù)形展示,但是官網(wǎng)組件沒(méi)有?,F(xiàn)在簡(jiǎn)單封裝一個(gè)組件在app中使用,可以無(wú)線(xiàn)嵌套,展開(kāi),收縮,獲取子節(jié)點(diǎn)數(shù)據(jù)等。
簡(jiǎn)單效果
組件TreeData
<template> <view class="tree"> <template v-for="(node, index) in treeData"> <view> <span @click="toggleNode($event, node)"> <uni-icons v-if="node.children && node.children.length > 0" :type="node.expanded ? 'arrowdown' : 'arrowright'" size="14" ></uni-icons> {{ node.label }} </span> <span @click.stop="deleteNode($event, node)" class="action-button delete-button" >刪除</span > <span @click.stop="editNode($event, node)" class="action-button edit-button" >編輯</span > <view v-if="node.expanded" class="children"> <Tree :treeData="node.children" @edit-node="(childNode) => $emit('edit-node', childNode)" @delete-node="(childNode) => $emit('delete-node', childNode)" /> </view> </view> </template> </view> </template> <script> export default { name: "Tree", props: { treeData: { type: Array, default: () => [], }, expandAll: { type: Boolean, default: true, }, }, data() { return { init: false, }; }, watch: { treeData: { immediate: true, handler(newData) { if (!this.init) { this.initializeTreeData(newData, this.expandAll); this.init = true; } }, }, }, methods: { initializeTreeData(nodes, expanded) { nodes.forEach((node) => { this.$set(node, "expanded", expanded); // 使用 $set 確保響應(yīng)式 if (node.children && node.children.length > 0) { this.initializeTreeData(node.children, expanded); // 遞歸處理子節(jié)點(diǎn) } }); }, toggleNode(event, node) { event.stopPropagation(); // 阻止事件冒泡 node.expanded = !node.expanded; // 切換節(jié)點(diǎn)展開(kāi)狀態(tài) }, editNode(event, node) { event.stopPropagation(); this.$emit("edit-node", node); // 觸發(fā)父組件的 edit-node 事件,并傳遞當(dāng)前節(jié)點(diǎn) }, deleteNode(event, node) { event.stopPropagation(); this.$emit("delete-node", node); // 觸發(fā)父組件的 delete-node 事件,并傳遞當(dāng)前節(jié)點(diǎn) }, }, }; </script> <style scoped> .tree { padding-left: 15px; } .children { padding-left: 15px; } .tree-node { display: flex; align-items: center; } .action-button { cursor: pointer; margin-left: 10px; color: #409eff; } .edit-button { float: right; } .delete-button { float: right; } </style>
在頁(yè)面中使用...
<template> <view class="page"> <Tree :treeData="treeData" :expandAll="expandAll" @edit-node="handleEditNode" @delete-node="handleDeleteNode" /> </view> </template> <script> import Tree from "@/components/TreeData"; export default { components: { Tree, }, data() { return { treeData: [ { label: "根節(jié)點(diǎn) 1", children: [ { label: "子節(jié)點(diǎn) 1-1", children: [ { label: "子節(jié)點(diǎn) 1-1-1", children: [], }, { label: "子節(jié)點(diǎn) 1-1-2", children: [], }, ], }, { label: "子節(jié)點(diǎn) 1-2", children: [], }, ], }, { label: "根節(jié)點(diǎn) 2", children: [ { label: "子節(jié)點(diǎn) 2-1", children: [], }, ], }, ], expandAll: true, // 控制是否全部展開(kāi) }; }, methods: { handleEditNode(node) { console.log("編輯節(jié)點(diǎn)", node); // 處理編輯節(jié)點(diǎn)的邏輯 }, handleDeleteNode(node) { console.log("刪除節(jié)點(diǎn)", node); // 處理刪除節(jié)點(diǎn)的邏輯 }, }, }; </script> <style scoped> page { background-color: #f5f6f8; } .page { padding: 20px; } </style>
到此這篇關(guān)于uni-app無(wú)限級(jí)樹(shù)形組件簡(jiǎn)單實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)uni-app無(wú)限級(jí)樹(shù)形組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- uni-app封裝組件實(shí)現(xiàn)下方滑動(dòng)彈出模態(tài)框效果
- uni-app自定義組件和通信的方式
- uni-app項(xiàng)目中引入Vant?UI組件庫(kù)完美避坑指南(純凈版)
- Uni-App用uView組件庫(kù)中u-picker實(shí)現(xiàn)地區(qū)的省-市-區(qū)三級(jí)聯(lián)動(dòng)、確認(rèn)及回顯
- uni-app中Navigator組件的用法詳解及傳參方式
- uni-app使用組件的步驟記錄
- uni-app自定義組件components導(dǎo)入失敗或頁(yè)面不顯示文本等解決方法
- 在uni-app中使用vant組件的方法
相關(guān)文章
vueJS簡(jiǎn)單的點(diǎn)擊顯示與隱藏的效果【實(shí)現(xiàn)代碼】
下面小編就為大家?guī)?lái)一篇vueJS簡(jiǎn)單的點(diǎn)擊顯示與隱藏的效果【實(shí)現(xiàn)代碼】。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,一起跟隨小編過(guò)來(lái)看看吧2016-05-05基于Vue2x實(shí)現(xiàn)響應(yīng)式自適應(yīng)輪播組件插件VueSliderShow功能
本文講述的是從開(kāi)發(fā)一款基于Vue2x的響應(yīng)式自適應(yīng)輪播組件插件的一個(gè)全過(guò)程,包含發(fā)布到npm,構(gòu)建自己的npm包,供下載安裝使用的技巧,非常不錯(cuò),具有一定的參考借鑒價(jià)值,感興趣的朋友跟隨腳本之家小編一起看看吧2018-05-05vue項(xiàng)目打包后網(wǎng)頁(yè)的title亂碼解決方案
這篇文章主要介紹了vue項(xiàng)目打包后網(wǎng)頁(yè)的title亂碼解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03Vue項(xiàng)目打包壓縮的實(shí)現(xiàn)(讓頁(yè)面更快響應(yīng))
這篇文章主要介紹了Vue項(xiàng)目打包壓縮的實(shí)現(xiàn)(讓頁(yè)面更快響應(yīng)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03詳解如何實(shí)現(xiàn)Element樹(shù)形控件Tree在懶加載模式下的動(dòng)態(tài)更新
這篇文章主要介紹了詳解如何實(shí)現(xiàn)Element樹(shù)形控件Tree在懶加載模式下的動(dòng)態(tài)更新,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04利用Vue3實(shí)現(xiàn)一個(gè)可以用js調(diào)用的組件
最近遇到個(gè)功能要求,想要在全局中調(diào)用組件,而且要在某些js文件內(nèi)調(diào)用,所以這篇文章主要給大家介紹了關(guān)于如何利用Vue3實(shí)現(xiàn)一個(gè)可以用js調(diào)用的組件的相關(guān)資料,需要的朋友可以參考下2021-08-08VueX瀏覽器刷新如何實(shí)現(xiàn)保存數(shù)據(jù)
這篇文章主要介紹了VueX瀏覽器刷新如何實(shí)現(xiàn)保存數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07vue使用路由守衛(wèi)實(shí)現(xiàn)菜單的權(quán)限設(shè)置
我們使?vue-element-admin前端框架開(kāi)發(fā)后臺(tái)管理系統(tǒng)時(shí),?般都會(huì)涉及到菜單的權(quán)限控制問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于vue使用路由守衛(wèi)實(shí)現(xiàn)菜單的權(quán)限設(shè)置的相關(guān)資料,需要的朋友可以參考下2023-06-06