uniapp组件实现省市区三级联动选择

1.导入插件

先将uni-data-picker组件导入我们的HBuilder项目中,在DCloud插件市场搜索uni-data-picker

点击下载插件并导入到我们的项目中

2.组件调用

curLocation :获取到的当前位置(省市区)

javascript 复制代码
<uni-data-picker v-slot:default="{data, error, options}" :localdata="localData" popup-title="请选择省市区" @change="onchange" @nodeclick="onnodeclick">
						<view class="selectedAddress">
							<view v-if="data.length == 0 && curLocation">{{ curLocation }}</view>
							<view v-if="data.length" class="selected">
								<view v-for="(item,index) in data" :key="index" class="selected-item">
								<text>{{item.text}} </text> 
								</view>
							</view>
							<view class="addrlocation">
								<uni-icons type="location" color="#ec4149" size="24"></uni-icons>
							</view>
						</view>
					</uni-data-picker>
javascript 复制代码
data(){
    return {
        localData:[], //省市区地址
		curLocation: uni.getStorageSync('location_address'),
    }
}

3.处理我们需要的省市区数据

1)在https://gitee.com/dcloud/opendb下载省市区源数据,collection/opendb-city-china

2)下载后的数据是一组一维对象数组,接下来把这个数组处理成树形结构

在页面中引入:

javascript 复制代码
const cityRows = require('@/common/opendb-master/collection/opendb-city-china/data.json')
javascript 复制代码
// 省市区数据树生成
			get_city_tree () {
				let res = []
				if (cityRows.length) {
					// 递归生成
					res = this.handleTree(cityRows)
				}
				return res
			},


handleTree (data, parent_code = null) {
				let res = []
				
				let keys = {
					id: 'code',
					pid: 'parent_code',
					children: 'children',
					
					text: 'name',
					value: 'code'
				}
				
				let oneItemDEMO = {
					text: '',
					value: '',
					children: []
				}
				let oneItem = {}
				
				// 循环
				for (let index in data) {
					// 判断
					if (parent_code === null) {
						// 顶级菜单 - 省
						if (!data[index].hasOwnProperty( keys.pid ) || data[index][keys.pid] == parent_code) {
							// 不存在parent_code,或者已匹配
							oneItem = JSON.parse(JSON.stringify(oneItemDEMO))
							oneItem.text = data[index][keys.text]
							oneItem.value = data[index][keys.value]
							
							// 递归下去
							oneItem.children = this.handleTree(data, data[index][keys.id])
							res.push(oneItem)
						} 
					} else {
						// 非顶级菜单 - 市、区、街道
						if (data[index].hasOwnProperty( keys.pid ) && data[index][keys.pid] == parent_code) {
							// 已匹配
							oneItem = JSON.parse(JSON.stringify(oneItemDEMO))
							oneItem.text = data[index][keys.text]
							oneItem.value = data[index][keys.value]
							
							// 递归下去
							oneItem.children = this.handleTree(data, data[index][keys.id])
							res.push(oneItem)
							
						}
					}
				}
				return res
			},
javascript 复制代码
onLoad(options){
    this.localData = this.get_city_tree()
}

最后的效果:

相关推荐
满栀58518 小时前
vue3动态路由详细效果
前端·javascript·vue.js·typescript·前端框架
使用小功能大师19 小时前
从零搭建高可用Web应用:全栈架构实战与成本优化完全指南
前端·阿里云·架构·服务搭建
IT_陈寒19 小时前
SpringBoot这个特性差点让我加班到凌晨
前端·人工智能·后端
2401_8949155319 小时前
Geo 优化源码部署避坑指南:解决访问异常、定位失效、收录卡顿问题
java·服务器·后端·缓存·开源
Dovis(誓平步青云)19 小时前
《 固井工程软件 Cemsol 的数据管理与国产化适配实践》
android·java·开发语言·人工智能
qq_2676128919 小时前
Gitee DevOps的度量驱动持续改进:从构建数据到交付效率的量化管理与优化路径
前端·gitee·自动化
大模型码小白19 小时前
AI安全前沿:AI大模型安全防护的前沿技术
java·网络·人工智能·python·深度学习·学习·安全
meilindehuzi_a19 小时前
从 Vite 到 Axios 与 Mock:React Todos 全栈项目架构及请求链路详解
前端·react.js·架构
evans在进步19 小时前
LeetCode 34:在排序数组中查找元素的首尾位置——Java 两次二分查找详解
java·python·leetcode
亿元程序员19 小时前
项目经理让我在 2D 项目里做 3D 音乐球球,我只好做了个“假的”
前端