uniapp H5实现省市区地区本地选择器

在这里插入图片描述

  1. 引入资源文件region.json,在git下载,我这边选择的是省市区
    在这里插入图片描述

在这里插入图片描述
2. 页面代码
先引入资源文件,再对文件进行初始处理即可

<template>
	<view class="content">
				<view class="form_item">
					<view class="title">地区</view>
					<picker mode="multiSelector" @change="okReginon" @columnchange="bindMultiPickerColumnChange" :value="regionIndex" :range="region">
						<view v-if="person.province" class="inp">{{person.province}}{{person.city}}{{person.area}}</view>
						<view v-else class="inp pla">请选择地区</view>
					</picker>
				</view>
		</view>
	</view>
</template>

<script>
	import provinceData from '../../static/region.json';

	export default {
		data() {
			return {
				// 省市区选择器
				region: [
					[],
					[],
					[],
				],
				regionIndex: [0, 0, 0],
				person: {
					"province": null,
					"city": null,
					"area": null,
				},
			}
		},
		onLoad() {
			console.log(provinceData)
			provinceData.forEach((item,index) =>{
				this.region[0] = [...this.region[0],item.name]
			})
			provinceData[0].children.forEach(item =>{
				this.region[1] = [...this.region[1],item.name]
			})
			provinceData[0].children[0].children.forEach(item =>{
				this.region[2] = [...this.region[2],item.name]
			})
		},
		methods: {
			okReginon(e){
				console.log(e.detail.value)
				this.regionIndex = e.detail.value
				this.person.province = this.region[0][this.regionIndex[0]]
				this.person.city = this.region[1][this.regionIndex[1]]
				this.person.area = this.region[2][this.regionIndex[2]]
			},
			bindMultiPickerColumnChange: function(e) {
				console.log('修改的列为:' + e.detail.column + ',值为:' + e.detail.value)
				console.log(this.region[e.detail.column][e.detail.value])
				// 省
				if(e.detail.column == 0){
					this.regionIndex[0] = e.detail.value
					this.region[1] = []
					this.region[2] = []
					provinceData[e.detail.value].children.forEach(item =>{
						this.region[1] = [...this.region[1],item.name]
					})
					provinceData[this.regionIndex[0]].children[0].children.forEach(item =>{
						this.region[2] = [...this.region[2],item.name]
					})
				}
				// 市
				else if(e.detail.column == 1){
					console.log(this.regionIndex[0])
					this.regionIndex[1] = e.detail.value
					this.region[2] = []
					provinceData[this.regionIndex[0]].children[e.detail.value].children.forEach(item =>{
						this.region[2] = [...this.region[2],item.name]
					})
				}else{
					this.regionIndex[2] = e.detail.value
				}
				this.$forceUpdate()
			},

		}
	}
</script>

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值