uniapp封装三级联动省市区(可以进行数据回填)

直接上代码

<view class="" @touchmove.stop.prevent="preventTouchMove">
			<picker-view class="address-picker-box" :indicator-style="indicatorStyle" :value="pcaIndex"
				@change="bindChange">
				<picker-view-column class="center">
					<div class="address-picker-item center" v-for="(item,index) in provinceList" :key="index">
						<text class="address-picker-item-text">{{item.shortName}}</text>
					</div>
				</picker-view-column>
				<picker-view-column>
					<div class="address-picker-item center" v-for="(item,index) in provinceList[pcaIndex[0]].children"
						:key="index">
						<text class="address-picker-item-text">{{item.shortName}}</text>
					</div>
				</picker-view-column>
				<picker-view-column>
					<div class="address-picker-item center"
						v-for="(item,index) in provinceList[pcaIndex[0]].children[pcaIndex[1]].children" :key="index">
						<text class="address-picker-item-text">{{item.shortName}}</text>
					</div>
				</picker-view-column>
			</picker-view>
		</view>
城市格式
[
{
children:[
children:[
{
shortName:'东城''
},
....
]
shortName:'北京''
]
shortName:'北京''
}
]
格式就是嵌套

jsd代码

data(){
return {
provinceList: uni.getStorageSync('cityData'), // 省列表一定要提前去获取数据,可以在上个页面本地缓存,
pcaIndex: [0, 0, 0],//这个可以设置会填的数据,根据索引,
}
},
methods:{
//监听
	bindChange(e) {
				let currentIndex = e.detail.value;
				if (currentIndex[0] != this.pcaIndex[0]) {
					this.pcaIndex.splice(0, 3, currentIndex[0], 0, 0);
					return;
				}
				if (currentIndex[1] != this.pcaIndex[1]) {
					this.pcaIndex.splice(1, 2, currentIndex[1], 0);
					return;
				}
				if (currentIndex[2] != this.pcaIndex[2]) {
					this.pcaIndex.splice(2, 1, currentIndex[2]);
					return;
				}
			},
}

《程序员》曾陪伴了无数开发者成长。《新程序员》全新归来,推荐给大家!

里面东西不太全,不懂得,可以私信我
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陌寒1024

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值