Vue 樹狀結構控件

1、效果圖如下所示:

2、網絡請求的數據結構如下:

3、新建插件文件:menu-tree.vue,插件代碼如下:

<template><div class="root"><div class="parent" @click="onParentClick(parentItem)"><div class="parent-rectangle" :style="{ 'width': i == 1 ? '24px' : '32px' }" v-for="i in currentLevel - 1" :key="i"></div><div class="parent-right"><img class="parent-icon" :src="getParentIconUrl()" @click.stop="changeExpandStatus()"/><div class="parent-title" v-html="parentItem.text"></div></div></div><div class="child" v-if="isShowChildList()"><MenuTree :parentItem="item" v-for="(item, index) in parentItem.children" :currentLevel="currentLevel + 1":key="index" @onMenuTreeItemConfirm="onMenuTreeItemConfirm"></MenuTree></div></div>
</template><script>
export default {components: {MenuTree: () => import('./menu-tree.vue') // 引入自身作為子組件},props: {parentItem: {type: Object,default: {}},currentLevel: {type: Number,default: 1},},mounted() {},data() {return {treeMinUnitIconUrl: require('@/assets/images/icon-file.svg'),treeFoldedIconUrl: require('@/assets/images/icon-to-fold.svg'),treeExpandedIconUrl: require('@/assets/images/icon-to-expand.svg'),}},watch: {},methods: {isShowChildList() {return this.parentItem.expanded;},changeExpandStatus() {console.log("this.parentItem.childQty = " + this.parentItem.childQty);if (this.parentItem.childQty && this.parentItem.childQty > 0) {this.parentItem.expanded = !this.parentItem.expanded;}},getParentIconUrl() {if (this.currentLevel == 1) {return this.treeMinUnitIconUrl;}if (!this.parentItem.childQty || this.parentItem.childQty == 0) {return this.treeMinUnitIconUrl;}return this.parentItem.expanded ? this.treeFoldedIconUrl : this.treeExpandedIconUrl;},onParentClick(item) {if (this.currentLevel > 1) {item.currentLevel = this.currentLevel;}this.$emit("onMenuTreeItemConfirm", item);},onMenuTreeItemConfirm(item) {this.onParentClick(item);},},
}
</script><style scoped>
.root {width: 100%;min-height: 48px;display: flex;flex-direction: column;align-items: center;
}.parent {min-height: 48px;width: 100%;display: flex;flex-direction: row;align-items: center;
}.parent-rectangle {min-height: 48px;
}.parent-right {min-height: 48px;min-width: 140px;display: flex;flex-direction: row;align-items: center;
}.parent-icon {width: 24px;height: 24px;
}.parent-title {width: calc(100% - 24px - 8px);min-height: 16px;text-align: left;line-height: 16px;font-size: 14px;margin-left: 8px;
}.child {width: 100%;display: flex;flex-direction: column;align-items: center;
}
</style>

4、使用方法如下:

<template><div class="root"><van-popup class="popup" v-model="showMenuTreePop" round position="bottom"><div class="popup-title">請選擇行政區劃</div><div style="height: 60vh; width: 100%; overflow-y: scroll;"><MenuTree :parentItem="rootItem" :currentLevel="1" @onMenuTreeItemConfirm="onMenuTreeItemConfirm"></MenuTree></div><div class="table-bottom"><div class="table-bottom-btn color-green" @click="showMenuTreePop = false">取消</div></div></van-popup></div>
</template><script>
import MenuTree from '@/components/menu-tree.vue';
export default {components: { MenuTree },data() {return {rootItem: {},showMenuTreePop: false,}},methods: {loadMenutreeDataList() {// 通過網絡請求獲取數據列表 resconsole.log("res = ", res);this.$set(this.rootItem, "children", res);this.$set(this.rootItem, "text", "行政區劃");this.$set(this.rootItem, "expanded", true);this.$set(this.rootItem, "childQty", res ? res.length : 0);this.showMenuTreePop = true;// 打開彈窗},onMenuTreeItemConfirm(clickedItem) {// clickedItem 就是點擊選中的數據項, 其 currentLevel 屬性值就是其所處層級,如果層級是1,則表示點擊的是根節點this.showMenuTreePop = false;// 關閉彈窗},}
}
</script><style scoped>
.root {width: 100%;height: 100vh;
}.popup {width: 100%;background: #FFFFFF;border-radius: 16px;
}.table-bottom {width: 100%;height: 71px;display: flex;flex-direction: row;align-items: center;border-top: solid 1px #E8E8E8;
}.table-bottom-btn {min-width: 50px;height: 44px;flex: 1;color: #FFFFFF;font-size: 12px;line-height: 44px;border-radius: 22px;margin-left: 3%;margin-right: 3%;font-family: PingFangSC-Regular;font-weight: 400;text-align: center;
}.color-green {color: #FFFFFF;background-color: #2DA541;
}
</style>

5、插件中使用到的資源文件如下:

1、icon-file.svg<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1716600220842" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="19100" xmlns:xlink="http://www.w3.org/1999/xlink" width="48" height="48"><path d="M815.104 69.632q27.648 25.6 44.032 42.496t25.088 28.672 10.752 19.968 2.048 14.336l0 16.384-151.552 0q-10.24 0-17.92-7.68t-12.8-17.92-7.68-20.992-2.56-16.896l0-126.976 3.072 0q8.192 0 16.896 2.56t19.968 9.728 28.16 20.48 42.496 35.84zM640 129.024q0 20.48 6.144 42.496t19.456 40.96 33.792 31.232 48.128 12.288l149.504 0 0 577.536q0 29.696-11.776 53.248t-31.232 39.936-43.008 25.6-46.08 9.216l-503.808 0q-19.456 0-42.496-11.264t-43.008-29.696-33.28-41.984-13.312-49.152l0-696.32q0-21.504 9.728-44.544t26.624-42.496 38.4-32.256 45.056-12.8l391.168 0 0 128zM704.512 768q26.624 0 45.056-18.944t18.432-45.568-18.432-45.056-45.056-18.432l-384 0q-26.624 0-45.056 18.432t-18.432 45.056 18.432 45.568 45.056 18.944l384 0zM768 448.512q0-26.624-18.432-45.568t-45.056-18.944l-384 0q-26.624 0-45.056 18.944t-18.432 45.568 18.432 45.056 45.056 18.432l384 0q26.624 0 45.056-18.432t18.432-45.056z" p-id="19101" fill="#1296db"></path></svg>2、icon-to-expand.svg<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1716600138280" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5120" xmlns:xlink="http://www.w3.org/1999/xlink" width="48" height="48"><path d="M880 112a32 32 0 0 1 32 32v736a32 32 0 0 1-32 32H144a32 32 0 0 1-32-32V144a32 32 0 0 1 32-32z m-40 72H184v656h656V184zM536 320c4.416 0 8 3.584 8 8V480h152c4.416 0 8 3.584 8 8v48a8 8 0 0 1-8 8H544v152a8 8 0 0 1-8 8h-48a8 8 0 0 1-8-8V544H328A8 8 0 0 1 320 536v-48c0-4.416 3.584-8 8-8H480V328c0-4.416 3.584-8 8-8z" fill="#1296db" fill-opacity=".65" p-id="5121"></path></svg>3、icon-to-fold.svg<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1716600168259" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="18002" xmlns:xlink="http://www.w3.org/1999/xlink" width="48" height="48"><path d="M880 112a32 32 0 0 1 32 32v736a32 32 0 0 1-32 32H144a32 32 0 0 1-32-32V144a32 32 0 0 1 32-32z m-40 72H184v656h656V184zM696 480c4.416 0 8 3.584 8 8v48a8 8 0 0 1-8 8h-368A8 8 0 0 1 320 536v-48c0-4.416 3.584-8 8-8z" fill="#1296db" fill-opacity=".65" p-id="18003"></path></svg>

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/83570.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/83570.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/83570.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

洛谷P12610 ——[CCC 2025 Junior] Donut Shop

題目背景 Score: 15. 題目描述 The owner of a donut shop spends the day baking and selling donuts. Given the events that happen over the course of the day, your job is to determine the number of donuts remaining when the shop closes. 輸入格式 The first …

數據挖掘頂刊《IEEE Transactions on Knowledge and Data Engineering》2025年5月研究熱點都有些什么?

本推文對2025年5月出版的數據挖掘領域國際頂級期刊《IEEE Transactions on Knowledge and Data Engineering》進行了分析&#xff0c;對收錄的62篇論文的關鍵詞與研究主題進行了匯總&#xff0c;并對其中的研究熱點進行了深入分析&#xff0c;希望能為相關領域的研究人員提供有…

華為OD機試真題——最小的調整次數/特異性雙端隊列(2025B卷:100分)Java/python/JavaScript/C++/C語言/GO六種最佳實現

2025 B卷 100分 題型 本文涵蓋詳細的問題分析、解題思路、代碼實現、代碼詳解、測試用例以及綜合分析; 并提供Java、python、JavaScript、C++、C語言、GO六種語言的最佳實現方式! 2025華為OD真題目錄+全流程解析/備考攻略/經驗分享 華為OD機試真題《最小的調整次數/特異性雙端…

2024年ESWA SCI1區TOP,自適應學習灰狼算法ALGWO+無線傳感器網絡覆蓋優化,深度解析+性能實測

目錄 1.端午快樂2.摘要3.灰狼算法GWO原理4.改進策略5.結果展示6.參考文獻7.代碼獲取8.讀者交流 1.端午快樂 今天端午節&#xff0c;祝各位朋友端午安康&#xff0c;闔家平安&#xff01; 2.摘要 無線傳感器網絡&#xff08;WSNs&#xff09;是一種被廣泛應用的新興技術&…

ADI硬件筆試面試題型解析下

本專欄預計更新60期左右。當前第17期-ADI硬件. ADI其硬件工程師崗位的招聘流程通常包括筆試和多輪技術面試,考察領域涵蓋模擬電路設計、數字電路、半導體器件和信號處理等。 本文通過分析平臺上的信息,匯總了ADI硬件工程師的典型筆試和面試題型,并提供詳細解析和備考建議,…

SpringCloud 分布式鎖Redisson鎖的重入性與看門狗機制 高并發 可重入

可重入 Redisson 的鎖支持 可重入性&#xff0c;這意味著同一個線程在獲取鎖后&#xff0c;如果再次嘗試獲取該鎖&#xff0c;它可以成功地獲得鎖&#xff0c;而不會被阻塞。 每次一個線程成功獲取鎖后&#xff0c;它的持有次數會增加。當線程再次獲取該鎖時&#xff0c;Redi…

Java 中 Redis 過期策略深度解析(含拓展-redis內存淘汰策略列舉)

&#x1f91f;致敬讀者 &#x1f7e9;感謝閱讀&#x1f7e6;笑口常開&#x1f7ea;生日快樂?早點睡覺 &#x1f4d8;博主相關 &#x1f7e7;博主信息&#x1f7e8;博客首頁&#x1f7eb;專欄推薦&#x1f7e5;活動信息 文章目錄 Java 中 Redis 過期策略深度解析一、Redis 過…

Flutter - 原生交互 - 相機Camera - 01

環境 Flutter 3.29 macOS Sequoia 15.4.1 Xcode 16.3 集成 Flutter提供了camera插件來拍照和錄視頻&#xff0c;它提供了一系列可用的相機&#xff0c;并使用特定的相機展示相機預覽、拍照、錄視頻。 添加依賴 camera: 提供使用設備相機模塊的工具path_provider: 尋找存儲圖…

基于 Amazon Q Developer CLI 和 Amazon Bedrock Knowledge Bases 實現智能問答系統

1. 引言 傳統企業通常將常見問題&#xff08;FAQ&#xff09;發布在網站上&#xff0c;方便客戶自助查找信息。然而&#xff0c;隨著生成式 AI 技術的迅速發展與商業滲透&#xff0c;這些企業正積極探索構建智能問答系統的新途徑。這類系統不僅能顯著提升客戶體驗&#xff0c;…

Go 為何天生適合云原生?

當前我們正處在 AI 時代&#xff0c;但是在基礎架構領域&#xff0c;仍然處在云原生時代。云原生仍然是當前時代的風口之一。作為一個 Go 開發者&#xff0c;職業進階的下一站就是學習云原生技術。作為 Go 開發者學習云原生技術有得天獨厚的優勢&#xff0c;這是因為 Go 天生適…

Mac查看MySQL版本的命令

通過 Homebrew 查看&#xff08;如果是用 Homebrew 安裝的&#xff09; brew info mysql 會顯示你安裝的版本、路徑等信息。 你的終端輸出顯示&#xff1a;你并沒有安裝 MySQL&#xff0c;只是查詢了 brew 中的 MySQL 安裝信息。我們一起來看下重點&#xff1a; &#x1f9fe…

Kafka ACK機制詳解:數據可靠性與性能的權衡之道

在分布式消息系統中&#xff0c;消息確認機制是保障數據可靠性的關鍵。Apache Kafka 通過 ACK&#xff08;Acknowledgment&#xff09;機制 實現了靈活的數據確認策略&#xff0c;允許用戶在 數據可靠性 和 系統性能 之間進行權衡。本文將深入解析 Kafka ACK 機制的工作原理、配…

FastMCP:構建 MCP 服務器和客戶端的高效 Python 框架

在人工智能領域&#xff0c;模型上下文協議&#xff08;Model Context Protocol&#xff0c;簡稱 MCP&#xff09;作為一種標準化的協議&#xff0c;為大型語言模型&#xff08;LLM&#xff09;提供了豐富的上下文和工具支持。而 FastMCP 作為構建 MCP 服務器和客戶端的 Python…

動態庫導出符號與extern “C“

1. windows下動態庫導出符號 根據C/C語法規則&#xff0c;函數聲明中的修飾符&#xff08;如__declspec(dllexport)&#xff09;可以放在返回類型之前或返回類型之后、函數名之前。這兩種方式在功能上是等價的&#xff0c;編譯器會以相同的方式處理。 __declspec(dllexport) …

Linux(9)——進程(控制篇——下)

目錄 三、進程等待 1&#xff09;進程等待的必要性 2&#xff09;獲取子進程的status 3&#xff09;進程的等待方法 wait方法 waitpid方法 多進程創建以及等待的代碼模型 非阻塞的輪訓檢測 四、進程程序替換 1&#xff09;替換原理 2&#xff09;替換函數 3&…

Datatable和實體集合互轉

1.使用已廢棄的 JavaScriptSerializer&#xff0c;且反序列化為弱類型 ArrayList。可用但不推薦。 using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Linq; using System.Reflection; using System.Web; using Sy…

阿里云服務器ECS詳解:云服務器是什么,云服務器優勢和應用場景及參考

云服務器ECS是阿里云眾多云產品中&#xff0c;最受用戶關注的產品&#xff0c;阿里云服務器提供多樣化的計算能力&#xff0c;支持x86、Arm架構&#xff0c;涵蓋CPU、GPU等多種服務器類型&#xff0c;滿足各種用戶需求。其便捷易用特性包括分鐘級交付、通用API和性能監控框架&a…

【Oracle】游標

個人主頁&#xff1a;Guiat 歸屬專欄&#xff1a;Oracle 文章目錄 1. 游標基礎概述1.1 游標的概念與作用1.2 游標的生命周期1.3 游標的分類 2. 顯式游標2.1 顯式游標的基本語法2.1.1 聲明游標2.1.2 帶參數的游標 2.2 游標的基本操作2.2.1 完整的游標操作示例 2.3 游標屬性2.3.1…

pikachu靶場通關筆記11 XSS關卡07-XSS之關鍵字過濾繞過(三種方法滲透)

目錄 一、源碼分析 1、進入靶場 2、代碼審計 3、攻擊思路 二、滲透實戰 1、探測過濾信息 2、注入Payload1 3、注入Payload2 4、注入Payload3 本系列為通過《pikachu靶場通關筆記》的XSS關卡(共10關&#xff09;滲透集合&#xff0c;通過對XSS關卡源碼的代碼審計找到安…

XML 元素:基礎、應用與優化

XML 元素:基礎、應用與優化 引言 XML(可擴展標記語言)作為一種數據交換的標準格式,廣泛應用于互聯網數據交換、數據存儲等領域。XML 元素是 XML 文檔的核心組成部分,本文將深入探討 XML 元素的概念、特性、應用以及優化方法。 一、XML 元素概述 1.1 XML 元素的定義 X…