当前位置: 首页 > news >正文

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

1.导入插件

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

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

2.组件调用

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

<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>
data(){return {localData:[], //省市区地址curLocation: uni.getStorageSync('location_address'),}
}

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

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

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

在页面中引入:

const cityRows = require('@/common/opendb-master/collection/opendb-city-china/data.json')
// 省市区数据树生成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},
onLoad(options){this.localData = this.get_city_tree()
}

最后的效果:

http://www.lryc.cn/news/479509.html

相关文章:

  • 【C++】异常处理机制(对运行时错误的处理)
  • C++ boost steady_timer使用介绍
  • JVM 由多个模块组成,每个模块负责特定的功能
  • ORACLE批量插入更新如何拆分大事务?
  • kafka+zookeeper的搭建
  • Spark中的宽窄依赖
  • 安装和运行开发微信小程序
  • 地图框架之mapbox——(五)
  • Hive 的数据类型
  • 2024下半年软考考后估分,快来预约!
  • 第8章 利用CSS制作导航菜单作业
  • 基于Spring Boot的船舶监造系统的设计与实现,LW+源码+讲解
  • linux强制修改mysql的root账号密码
  • CentOS系统查看CPU、内存、操作系统等信息
  • 针对解决前后端BUG的个人笔记
  • 5G时代已来:我们该如何迎接超高速网络?
  • 企业级-实现Redis封装层
  • SpringBoot使用ApplicationContext.getBean启动报空指针处理记录
  • MongoDB Shell 基本命令(三)聚合管道
  • Go语言的内置容器
  • HCIP考试怎样预约?随时可以考试吗?
  • 香港航空 阿里滑块 acw_sc__v3 分析
  • JS传统函数中常见的 this 绑定问题
  • 跨域问题以及使用vscode的LiveServer插件跨域访问
  • 现代Web开发:WebSocket 实时通信详解
  • 《深度学习》——深度学习基础知识(全连接神经网络)
  • nginx 部署2个相同的vue
  • 利用Java easyExcel库实现高效Excel数据处理
  • Vulnhub靶场 Metasploitable: 1 练习(上)
  • 《Python编程实训快速上手》第二天--列表与元组