uniapp使用uview UI,自定義級聯選擇組件

一、需求:

1.省市區級聯選擇,可多選
2.可以一鍵選擇某個區域下的所有數據
3.點擊省展開市,點擊市展開區,以此類推(可返回上一層或多層)
4.只獲取選擇的人

效果視頻

二、注意事項以及源碼

1.需要安裝uView UI組件庫,安裝地址uView UI官網

2.源碼,復制即用

<template><view class="container"><!-- 層級導航 --><view class="breadcrumb"><text class="breadcrumb-item" v-for="(item, index) in breadcrumbList" :key="index" @click="goBackToLevel(index)":class="{ 'breadcrumb-item-active': index === breadcrumbList.length - 1 }">{{ item }}<text v-if="index !== breadcrumbList.length - 1" class="breadcrumb-separator"> ></text></text></view><!-- 層級內容 --><view class="level-content"><view class="level-item" v-for="(item, index) in currentLevelData" :key="item.id"><!-- 將點擊事件移到內部元素,避免與復選框沖突 --><view class="level-item-left" @click="handleLevelItemClick(item)"><view class="level-icon" :style="{ backgroundColor: getLevelColor(item) }"><text class="level-icon-text">{{ getLevelCode(item) }}</text></view><view class="level-info"><text class="level-title">{{ item.name }}</text><text class="level-desc">{{ item.leaf ? item.emergencyPersonnel.phone : `${getAllLeafCount(item)} 個人員` }}</text></view></view><view class="select-checkbox"><u-checkbox :checked="isNodeSelected(item)" :indeterminate="isNodeIndeterminate(item)"@change="(value) => handleNodeSelect(item, value)" shape="circle" active-color="#4F46E5"></u-checkbox></view></view></view><!-- 底部操作欄 --><view class="footer-bar"><view class="selected-info"><u-icon name="account-fill" size="30" color="#4F46E5"></u-icon><text class="selected-count">已選擇 {{ selectedPersons.length }}</text></view><u-button type="primary" @click="toFormPage" :disabled="selectedPersons.length === 0"class="next-button">下一步</u-button></view></view>
</template><script>
export default {data() {return {// 人員數據結構(修改后:新增省份、簡化字段、常見姓名)personnelData: [{"id": "510000","name": "四川省","children": [{"id": "511400","name": "眉山市","children": [{"id": "市管理員","name": "市管理員","children": [{"id": "10","name": "趙六","children": [],"emergencyPersonnel": {"unitName": "市管理員","unitAddress": "眉山市政務服務中心","personName": "趙六","phone": "19162984018","provinceName": "四川省","cityName": "眉山市","countyName": ""},"leaf": true},{"id": "11","name": "孫七","children": [],"emergencyPersonnel": {"unitName": "市管理員","unitAddress": "眉山市政務服務中心","personName": "孫七","phone": "18180809001","provinceName": "四川省","cityName": "眉山市","countyName": ""},"leaf": true}],"emergencyPersonnel": null,"leaf": false}],"emergencyPersonnel": null,"leaf": false}],"emergencyPersonnel": null,"leaf": false},{"id": "440000","name": "廣東省","children": [{"id": "440100","name": "廣州市","children": [{"id": "天河區管理員","name": "天河區管理員","children": [{"id": "12","name": "周八","children": [],"emergencyPersonnel": {"unitName": "天河區管理員","unitAddress": "廣州市天河區政務中心","personName": "周八","phone": "18228880309","provinceName": "廣東省","cityName": "廣州市","countyName": "天河區"},"leaf": true},{"id": "13","name": "吳九","children": [],"emergencyPersonnel": {"unitName": "天河區管理員","unitAddress": "廣州市天河區政務中心","personName": "吳九","phone": "18990370720","provinceName": "廣東省","cityName": "廣州市","countyName": "天河區"},"leaf": true}],"emergencyPersonnel": null,"leaf": false},{"id": "海珠區管理員","name": "海珠區管理員","children": [{"id": "14","name": "鄭十","children": [],"emergencyPersonnel": {"unitName": "海珠區管理員","unitAddress": "廣州市海珠區政務中心","personName": "鄭十","phone": "15508337779","provinceName": "廣東省","cityName": "廣州市","countyName": "海珠區"},"leaf": true}],"emergencyPersonnel": null,"leaf": false}],"emergencyPersonnel": null,"leaf": false},{"id": "440300","name": "深圳市","children": [{"id": "南山區管理員","name": "南山區管理員","children": [{"id": "15","name": "錢十一","children": [],"emergencyPersonnel": {"unitName": "南山區管理員","unitAddress": "深圳市南山區政務中心","personName": "錢十一","phone": "15983336111","provinceName": "廣東省","cityName": "深圳市","countyName": "南山區"},"leaf": true}],"emergencyPersonnel": null,"leaf": false}],"emergencyPersonnel": null,"leaf": false}],"emergencyPersonnel": null,"leaf": false},{"id": "330000","name": "浙江省","children": [{"id": "330100","name": "杭州市","children": [{"id": "西湖區管理員","name": "西湖區管理員","children": [{"id": "16","name": "馮十二","children": [],"emergencyPersonnel": {"unitName": "西湖區管理員","unitAddress": "杭州市西湖區政務中心","personName": "馮十二","phone": "18783398823","provinceName": "浙江省","cityName": "杭州市","countyName": "西湖區"},"leaf": true},{"id": "17","name": "陳十三","children": [],"emergencyPersonnel": {"unitName": "西湖區管理員","unitAddress": "杭州市西湖區政務中心","personName": "陳十三","phone": "13547674447","provinceName": "浙江省","cityName": "杭州市","countyName": "西湖區"},"leaf": true}],"emergencyPersonnel": null,"leaf": false},{"id": "余杭區管理員","name": "余杭區管理員","children": [{"id": "18","name": "褚十四","children": [],"emergencyPersonnel": {"unitName": "余杭區管理員","unitAddress": "杭州市余杭區政務中心","personName": "褚十四","phone": "18160172259","provinceName": "浙江省","cityName": "杭州市","countyName": "余杭區"},"leaf": true}],"emergencyPersonnel": null,"leaf": false}],"emergencyPersonnel": null,"leaf": false}],"emergencyPersonnel": null,"leaf": false}],// 當前層級數據currentLevelData: [],// 層級導航路徑breadcrumbList: [],// 已選擇的人員selectedPersons: [],// 層級歷史記錄,用于返回levelHistory: []};},onLoad() {// 初始化層級數據(因personnelData改為數組,此處調整為加載所有省份)this.currentLevelData = this.personnelData;this.breadcrumbList = ["全國"];this.levelHistory = [this.currentLevelData];},methods: {// 獲取層級顏色getLevelColor(item) {if (item.leaf) {return '#E0E7FF';}const colors = ['#E0F2FE', '#DBEAFE', '#EFF6FF', '#F0FDF4', '#FEF3C7'];let hash = 0;for (let i = 0; i < item.name.length; i++) {hash = item.name.charCodeAt(i) + ((hash << 5) - hash);}return colors[Math.abs(hash) % colors.length];},// 獲取層級代碼getLevelCode(item) {if (item.leaf) {return item.name.charAt(0);}return item.name.substring(0, 2);},// 遞歸獲取某個節點下的所有葉子節點getAllLeafNodes(node) {let leafNodes = [];if (node.leaf) {leafNodes.push(node);} else if (node.children && node.children.length > 0) {node.children.forEach(child => {leafNodes = leafNodes.concat(this.getAllLeafNodes(child));});}return leafNodes;},// 計算某個節點下的葉子節點總數getAllLeafCount(node) {return this.getAllLeafNodes(node).length;},// 判斷某個節點的選中狀態isNodeSelected(node) {const leafNodes = this.getAllLeafNodes(node);if (node.leaf) {return this.selectedPersons.some(p => p.id === node.id);}return leafNodes.every(leaf =>this.selectedPersons.some(p => p.id === leaf.id));},// 判斷某個非葉子節點是否半選isNodeIndeterminate(node) {if (node.leaf) return false;const leafNodes = this.getAllLeafNodes(node);const selectedLeafCount = leafNodes.filter(leaf =>this.selectedPersons.some(p => p.id === leaf.id)).length;return selectedLeafCount > 0 && selectedLeafCount < leafNodes.length;},// 將葉子節點轉換為selectedPersons格式convertLeafToSelected(leafNode) {return {id: leafNode.id,name: leafNode.name,phone: leafNode.emergencyPersonnel.phone,unitName: leafNode.emergencyPersonnel.unitName,unitAddress: leafNode.emergencyPersonnel.unitAddress,provinceName: leafNode.emergencyPersonnel.provinceName,cityName: leafNode.emergencyPersonnel.cityName,countyName: leafNode.emergencyPersonnel.countyName};},// 處理節點選擇 - 修正事件參數問題handleNodeSelect(node, checked) {const leafNodes = this.getAllLeafNodes(node);if (checked) {// 選中操作:添加所有未選中的葉子節點leafNodes.forEach(leaf => {const isExist = this.selectedPersons.some(p => p.id === leaf.id);if (!isExist) {this.selectedPersons.push(this.convertLeafToSelected(leaf));}});} else {// 取消選中:移除所有相關葉子節點this.selectedPersons = this.selectedPersons.filter(p =>!leafNodes.some(leaf => leaf.id === p.id));}},// 處理層級項點擊handleLevelItemClick(item) {if (!item.leaf && item.children && item.children.length > 0) {// 判斷當前層級是否是最外層(全國層級)const isRootLevel = this.breadcrumbList.length === 1 && this.breadcrumbList[0] === "全國";if (!isRootLevel) {this.breadcrumbList.push(item.name);} else {// 從全國進入省份時,更新導航路徑this.breadcrumbList = ["全國", item.name];}this.currentLevelData = item.children;this.levelHistory.push(this.currentLevelData);}},// 返回上一級goBackToLevel(index) {if (index >= this.breadcrumbList.length - 1) return;// 調整導航路徑this.breadcrumbList = this.breadcrumbList.slice(0, index + 1);// 調整層級歷史this.levelHistory = this.levelHistory.slice(0, index + 1);// 更新當前層級數據this.currentLevelData = this.levelHistory[index];},// 前往表單頁面toFormPage() {console.log(this.selectedPersons);if (this.selectedPersons.length === 0) {this.$u.toast('請至少選擇一個人員');return;}}}
};
</script><style scoped>
/* 樣式保持不變 */
.container {background-color: #F8F8F8;min-height: 100vh;padding-bottom: 60px;
}.breadcrumb {padding: 12px 15px;background-color: white;display: flex;align-items: center;font-size: 14px;overflow-x: auto;white-space: nowrap;
}.breadcrumb-item {color: #6B7280;padding: 0 2px;
}.breadcrumb-item-active {color: #4F46E5;font-weight: 500;
}.breadcrumb-separator {margin: 0 4px;color: #D1D5DB;
}.level-content {background-color: white;padding-top: 10px;
}.level-item {display: flex;align-items: center;justify-content: space-between;padding: 12px 15px;border-bottom: 1px solid #F3F4F6;
}.level-item-left {display: flex;align-items: center;width: 80%;
}.level-icon {width: 36px;height: 36px;border-radius: 50%;display: flex;align-items: center;justify-content: center;
}.level-icon-text {font-size: 14px;font-weight: 500;color: #4F46E5;
}.level-info {margin-left: 12px;
}.level-title {font-size: 16px;color: #1F2937;
}.level-desc {font-size: 12px;color: #6B7280;margin-top: 2px;display: block;
}.select-checkbox {width: 20px;height: 20px;display: flex;align-items: center;justify-content: center;
}.footer-bar {position: fixed;bottom: 0;left: 0;right: 0;background-color: white;display: flex;align-items: center;justify-content: space-between;padding: 10px 15px;border-top: 1px solid #F3F4F6;}.selected-info {display: flex;align-items: center;color: #1F2937;font-size: 14px;
}.selected-count {margin-left: 5px;
}.next-button {width: 120px;border-radius: 20px;
}
</style>

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

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

相關文章

徐州服務器:機柜租用具體包含哪些內容?

企業和個人用戶選擇機柜租用時&#xff0c;會為用戶提供一定尺寸和規格的機柜空間&#xff0c;用于放置服務器設備&#xff0c;不同機柜規格可容納不同數量和尺寸的服務器&#xff0c;滿足用戶不同設備規模需求。提供穩定且充足的電力供應&#xff0c;確保服務器設備正常運行&a…

AI熱點周報(8.24~8.30):Grok 2.5開源,OpenAI Realtime正式商用,Meta或與OpenAI或Google合作?

名人說&#xff1a;博觀而約取&#xff0c;厚積而薄發。——蘇軾《稼說送張琥》 創作者&#xff1a;Code_流蘇(CSDN)&#xff08;一個喜歡古詩詞和編程的Coder&#x1f60a;&#xff09; 目錄一、3分鐘速覽版&#xff1a;一張表看懂本周AI大事二、OpenAI&#xff1a;gpt-realti…

Linux筆記---計算機網絡概述

1. 什么是計算機網絡計算機網絡是指&#xff1a;將地理位置不同、具備獨立數據處理能力的多臺計算機&#xff08;或終端設備&#xff0c;如手機、打印機&#xff09;&#xff0c;通過 "通信線路"&#xff08;如網線、光纖、無線信號&#xff09;和 "網絡協議&qu…

AGDO-BP+NSGAII梯度下降優化算法優化BP神經網絡+NSGAII多目標優化算法,三目標和四目標案例

目錄效果一覽基本介紹程序設計參考資料效果一覽 四目標效果 三目標效果 基本介紹 1.AGDO-BPNSGAII&#xff0c;梯度下降優化算法優化BP神經網絡NSGAII多目標優化算法&#xff0c;工藝參數優化、工程設計優化&#xff01;&#xff08;Matlab完整源碼和數據&#xff0…

Java8-21的核心特性以及用法

Java81. Lambda表達式??理解?&#xff1a;簡化匿名內部類&#xff0c;允許將函數作為方法參數傳遞。 ?用法?&#xff1a;(參數) -> {表達式或代碼塊} ?示例?&#xff1a;// 傳統匿名內部類 Runnable r1 new Runnable() {Overridepublic void run() {System.out.prin…

《投資-45》- 《我從達爾文那里學到的投資知識》的核心思想和觀點

《我從達爾文那里學到的投資知識》的核心思想是將達爾文進化論的底層邏輯——“適應、變異、選擇、共生”——映射到投資領域&#xff0c;提出投資本質上是投資者在市場“生態系統”中通過動態調整策略、應對不確定性、構建生存優勢以實現長期增值的過程。以下是其核心觀點的分…

c#:抽象類中的方法

在C#中&#xff0c;抽象類中的方法是否必須實現取決于方法的類型和派生類的性質&#xff1a;?抽象方法?必須聲明在抽象類中&#xff0c;且沒有方法體&#xff08;僅以分號結尾&#xff09;。?派生類必須實現所有抽象方法?&#xff0c;除非派生類本身也是抽象類。實現時需使…

ICCV 2025 | 清華IEDA提出GUAVA,單圖創建可驅動的上半身3D化身!實時、高效,還能捕捉細膩的面部表情和手勢。

從單張圖片重建高質量、可動畫化且面部與手部動作豐富的 3D 人體化身&#xff0c;應用前景廣闊。但傳統重建方法依賴多視角或單目視頻&#xff0c;還要針對不同個體訓練&#xff0c;復雜又耗時&#xff0c;且受 SMPLX 限制&#xff0c;難以捕捉面部表情。為解決這些問題&#x…

LC正弦波振蕩電路

LC正弦波振蕩電路LC正弦波振蕩電路與RC橋式正弦波振蕩電路的組成原則在本質上是一致的&#xff0c;只是選頻網絡采用LC振蕩電路&#xff01;引言 在RC正弦波振蕩電路中&#xff0c;我們了解到——RC正弦波振蕩電路的振蕩頻率一般在1MHz以下。為了得到1MHz以上的信號&#xff0c…

從網絡層接入控制過渡到應用層身份認證的過程

這個過程非常經典,它涉及到了現代企業網絡管理中幾項核心的安全和控制技術。簡單來說,這是一個從網絡層接入控制過渡到應用層身份認證的過程。 其核心原理是:先保證設備是合法的(加域),再保證使用設備的人是合法的(網頁認證)。 下面我為您詳細分解其中的技術原理: 第…

【筆記】float類型的精度有限,無法精確表示123456.6789

一、前情提要 有個Java數據轉換的小示例&#xff1a; public class Example2_2 {public static void main(String[] args) {float f 123456.6789f;System.out.printf("f%30.12f", f);} }輸出的結果是&#xff1a;123456.679687500000這里就發現了個問題&#xff0c;…

西安電子科技大學金融專碩復試線為325分,推薦報考!

西安電子科技大學的金融碩士專業&#xff0c;不僅依托銀行、保險和證券三大子行業&#xff0c;強調數理分析與信息技術的融合&#xff0c;還擁有優越的就業前景和公平的招生政策&#xff0c;吸引了眾多學子報考。西安電子科技大學西安電子科技大學“金融碩士專業”碩士點設立于…

阿里云創建自己的博客,部署wordpress

目錄 1. 基礎環境介紹與規劃 1.1 操作系統 1.2 數據庫環境 1.3 Web 服務器 1.4 PHP 環境 1.5 WordPress 應用環境 1.6 網絡與安全 1.7 基礎環境總覽表 2.安裝流程 2.1 準備環境 2.2 安裝 MySQL 5.7 2.3 安裝 PHP 及擴展 2.4 安裝 Nginx&#xff08;或 Apache&…

CoreShop微信小程序商城框架開啟多租戶-添加一個WPF客戶端以便進行上傳產品信息和圖片(6)

前幾天已將基于開源CoreShop框架的微信小程序開啟多租戶功能,還開發了一個輔助客戶端,已經完成了以下工作: 修改管理員表格,添加上所管理的店鋪列表,兩個產品信息表也全部加上所屬店鋪信息。 開發一個WPF客戶端,能與服務器登錄并能正常通信。尤其是添加了一個輔助類CoreH…

LabVIEW虛擬實驗平臺設計

?為突破實物實驗教學的時空限制&#xff0c;解決實驗設備不足、操作風險高等問題&#xff0c;設計基于專業軟件的虛擬實驗平臺&#xff0c;以 “信號與系統” 無失真傳輸實驗為實例&#xff0c;融合仿真與網絡技術&#xff0c;適配高校工科實驗教學&#xff0c;提升教學質量與…

PHP Composer 依賴管理完整指南 入門到精通

PHP Composer 依賴管理完整指南 入門到精通 Composer 改變了整個 PHP 開發生態&#xff0c;我用了 10 年&#xff0c;可以說它是 PHP 生態里最重要的工具&#xff0c;沒有之一。不過我和 Composer 的關系一開始并不順利——從剛接觸時的一臉懵逼&#xff0c;到后來真正理解它的…

【開題答辯全過程】以 基于SpringBoot的校園一卡通管理系統的設計與實現為例,包含答辯的問題和答案

個人簡介一名14年經驗的資深畢設內行人&#xff0c;語言擅長Java、php、微信小程序、Python、Golang、安卓Android等開發項目包括大數據、深度學習、網站、小程序、安卓、算法。平常會做一些項目定制化開發、代碼講解、答辯教學、文檔編寫、也懂一些降重方面的技巧。感謝大家的…

深度學習中常用的激活函數

參考文章&#xff1a; Activation Functions — All You Need To Know! | by Sukanya Bag | Analytics Vidhya | Medium 深度學習筆記&#xff1a;如何理解激活函數&#xff1f;&#xff08;附常用激活函數&#xff09; - 知乎 part-0_13.pdf 機器學習中的數學——激活函數&…

無惡意軟件勒索:Storm-0501如何轉向云原生攻擊

Storm-0501基于云的勒索攻擊鏈概覽 | 圖片來源&#xff1a;微軟微軟威脅情報部門最新研究顯示&#xff0c;以經濟利益為目的的威脅組織Storm-0501已將其勒索軟件攻擊模式從傳統本地部署系統顯著轉向云原生攻擊。報告警告稱&#xff0c;該組織"持續優化攻擊活動&#xff0c…

k8s--etcd

目錄 靜態pod 使用步驟&#xff1a; 關鍵特性說明&#xff1a; 常見使用場景&#xff1a; 案例&#xff1a; 環境變量 配置全景解析 實操&#xff1a;?? 查看etcd集群節點信息 查看集群健康狀態 數據庫操作命令 增加(put) 查詢(get) 刪除(del) 租約命令 添加…