uview2.0 实现省市区三级联动

文章描述了一个在Vue应用中使用u-picker组件展示和联动省份、城市和区县数据的过程,包括如何从API获取数据、如何根据一级和二级选择更新二级和三级选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

废话不多说直接上代码

	<u-picker :show="show" ref="uPicker" :columns="columns" @confirm="confirm" @cancel="show=false"keyName="name" @change="changeHandler"></u-picker>

columns: []需要再data里面定义

1.先要展示所有数据

onLoad(option) {	
			this.getarealist()
		},
methods:{
getarealist() {
				arealist().then(res => {
					let sheng = [];
					let shi = [];
					let qu = [];
					let origi = [];
					origi = res.data.datas.area_list;
					this.origiData = origi;

					for (const key in this.origiData) {
						if (this.origiData.hasOwnProperty(key)) {
							let data = {
								name: this.origiData[key].name,
								id: this.origiData[key].id
							}
							sheng.push(data);
							const province = this.origiData[key];
							for (const cityKey in province.list) {
								if (province.list.hasOwnProperty(cityKey)) {
									const city = province.list[cityKey];
									let data = {
										name: city.name,
										id: city.id
									}
									shi.push(data);
									for (const countyKey in city.list) {
										if (city.list.hasOwnProperty(countyKey)) {
											const county = city.list[countyKey];
											let data = {
												name: county.name,
												id: county.id
											}

											qu.push(data);
										}
									}
								}
							}
						}
					}

					this.columns.push(
						JSON.parse(JSON.stringify(sheng)),
						JSON.parse(JSON.stringify(shi)),
						JSON.parse(JSON.stringify(qu))
					);
					console.log('Columns:', this.columns);
				}).catch(error => {
					console.error('Error fetching area list:', error);
				});
			},
}

2.然后根据第一级和第二级的切换对应省市区

changeHandler(e) {
				console.log('Change event:', e);
				const {
					columnIndex,
					value,
					picker = this.$refs.uPicker
				} = e;

				if (columnIndex === 0) {
					console.log(value, '45454545')
					// picker为选择器this实例,变化第二列对应的选项

					for (const key in this.origiData) {
						if (this.origiData.hasOwnProperty(key)) {
							console.log(value[0]);
							if (value[0].name === this.origiData[key].name) {
								let shi = [];
								let flag = this.origiData[key].list;
								console.log(flag, 'flag');
								for (const cityKey in flag) {
									if (flag.hasOwnProperty(cityKey)) {
										const city = flag[cityKey];

										let data = {
											name: city.name,
											id: city.id
										}
										console.log(data, '99999999999999');
										shi.push(data);

										picker.setColumnValues(1, shi);
										console.log(shi, '市4444');



										const citiesArray = Object.values(
											flag); // Convert the values of the flag object into an array
										const firstCity = citiesArray[
											0]; // Access the first item in the array, which represents the first city
										const firstCityList = firstCity
											.list; // Access the 'list' property of the first city
										let qu = [];
										// Now you can loop through the districts of the first city and process them
										for (const key in firstCityList) {
											if (firstCityList.hasOwnProperty(key)) {
												const district = firstCityList[key];
												let data = {
													name: district.name,
													id: district.id
												}
												qu.push(data);
												console.log('区县数据', district);
											}
										}
										picker.setColumnValues(2, qu);


									}
								}
							}

						}

					}
				}
				if (columnIndex === 1) {
					for (const key in this.origiData) {
						if (this.origiData.hasOwnProperty(key)) {
							if (value[0].name === this.origiData[key].name) {
								let shi = [];
								let flag = this.origiData[key].list;
								console.log(flag, 'flag');
								for (const cityKey in flag) {
									if (flag.hasOwnProperty(cityKey)) {
										const city = flag[cityKey];
										shi.push(city.name, city.id);
										console.log(flag, '987654321');
										for (const key in flag) {
											if (flag.hasOwnProperty(key)) {
												const obj = flag[key].name;

												if (value[1].name == obj) {
													console.log(flag[key].list, );
													const districtList = flag[key].list;
													let qu = [];
													for (const districtKey in districtList) {
														if (districtList.hasOwnProperty(districtKey)) {
															const district = districtList[districtKey];
															let data = {
																name: district.name,
																id: district.id
															}
															qu.push(data);
															console.log('District=================', district);
														}
													}
													picker.setColumnValues(2, qu);

												}

											}
										}

									}
								}
							}
						}
					}


				}






			},

最后完整的效果就是这样

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

蒜苔肉丝

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

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

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

打赏作者

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

抵扣说明:

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

余额充值