uniapp实现调取接口实现三级城市联动选择(结合uview组件库)

7 篇文章 0 订阅

在这里插入图片描述

效果图如上:封装了一个三级联动的组件,样式优化很简单这里只是做了个主要功能。

代码如下:
1、组件部分

<template>
	<view style="margin-top: 50rpx;">
		<u-button type="info" @click="open">选择地址</u-button>
		<u-popup v-model="show" mode="bottom" :zoom='true' border-radius="30">
			<view style="height: 980rpx;" closeable>
				<view style="height: 100rpx;line-height: 100rpx;text-align: center;">
					选择地区
				</view>
				<view class="tab">
					<u-tabs :list="list" active-color='#BF6477' :is-scroll="false" :current="current"></u-tabs>
				</view>
				<view style="height: 30rpx;border-top: 2rpx solid #BF6477;margin-top: 10rpx;">
				</view>
				<view style="display: flex;">
					<view class="list-cell">
						<scroll-view scroll-y="true" style="width: 100%;height: 700rpx;" scroll-with-animation="true"
							:show-scrollbar="false">
							<view v-for="(item,index) in province" :key="index"
								:class="item.ischeck?'single_active':'single'" @click="navChange(item)">
								{{item.area_name}}
							</view>
						</scroll-view>
					</view>
					<view class="list-cell">
						<scroll-view scroll-y="true" style="width: 100%;height: 700rpx;" scroll-with-animation="true"
							:show-scrollbar="false">
							<view v-for="(item,index) in city" :key="index"
								:class="item.ischeck?'single_active':'single'" @click="cityChoose(item)">
								{{item.area_name}}
							</view>
						</scroll-view>
					</view>
					<view class="list-cell">
						<scroll-view scroll-y="true" style="width: 100%;height: 700rpx;" scroll-with-animation="true"
							:show-scrollbar="false">
							<view v-for="(item,index) in district" :key="index"
								:class="item.ischeck?'single_active':'single'" @click="districtchoose(item)">
								{{item.area_name}}
							</view>
						</scroll-view>
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		baseArea
	} from '@/servies/user.js'
	export default {
		data() {
			return {
				current: 0,
				list: [{
					name: '请选择'
				}, {
					name: '请选择'
				}, {
					name: '请选择',
				}, ],
				show: true,
				province: [],
				city: [],
				district: [],
				citycode: 0,
				finalcheck_province: {},
				finalcheck_city: {},
				finalcheck_district: {},
				// indexList:[]
			}
		},
		created() {
			baseArea(this.citycode).then(res => {
				this.province = res.map(v => {
					return {
						...v,
						ischeck: false
					}
				});
			})
		},
		methods: {
			change(index) {
				this.current = index;
			},
			open() {
				this.show = true
			},
			navChange(m) {
				this.province.forEach(v => v.ischeck = false);
				const findindex = this.province.findIndex(v => v.area_name === m.area_name);
				this.province[findindex].ischeck = true;
				this.current = 1;
				if (this.citycode === 0) {
					this.list[0].name = m.area_name;
				};
				this.citycode = 1;
				this.finalcheck_province = m;
				baseArea(m.area_id).then(res => {
					this.city = res.map(item => {
						return {
							...item,
							ischeck: false
						}
					});
				})

			},
			cityChoose(m) {
				this.city.forEach(v => v.ischeck = false);
				const findindex = this.city.findIndex(v => v.area_name === m.area_name);
				this.city[findindex].ischeck = true;
				this.current = 2;
				if (this.citycode === 1) {
					this.list[1].name = m.area_name
				};
				this.finalcheck_city = m;
				this.citycode = 2;
				baseArea(m.area_id).then(res => {
					this.district = res.map(v => {
						return {
							...v,
							ischeck: false
						}
					});
				})

			},
			districtchoose(m) {
				this.district.forEach(v => v.ischeck = false);
				const findindex = this.district.findIndex(v => v.area_name === m.area_name);
				this.district[findindex].ischeck = true;
				this.finalcheck_district = m;
				if (this.citycode === 2) {
					this.list[2].name = m.area_name
				};
				this.show = false;
				const final = [
					this.finalcheck_province, this.finalcheck_city, this.finalcheck_district
				];
				this.$emit('change', {
					result: final
				})
			},

		}

	}
</script>

<style lang="less" scoped>
	.list-cell {
		width: 220rpx;
		height: 700rpx;
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		overflow: hidden;
		overflow-y: true;

		.single {
			width: 100%;
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
		}

		.single_active {
			width: 100%;
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			color: red;
		}

	}

	.tab {
		width: 550rpx;
		height: 80rpx;
		background-color: red;

	}
</style>

2、页面引用

```javascript
```<chooseAddr @change="change2"/>
import chooseAddr from '@/components/pickerAddress.vue'
	components: {
			chooseAddr
		},
	change2(res){
				console.log(res);
				uni.showToast({
					title:res.result[0].area_name+res.result[1].area_name+res.result[2].area_name,
					duration:2000,
					icon:'none'
				})
				
			},
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Uniapp 中可以使用 uview-ui 组件中的 `u-picker` 组件实现省市区三级联动功能。以下是一个简单的示例: 1. 首先,确保你已经安装了 uview-ui 组件。可以通过 npm 安装或直接下载源码引入。 2. 在需要使用省市区三级联动的页面中,引入 `u-picker` 组件: ```vue <template> <view> <u-picker :data="provinceList" v-model="selectedProvince" @change="handleProvinceChange"></u-picker> <u-picker :data="cityList" v-model="selectedCity" @change="handleCityChange"></u-picker> <u-picker :data="districtList" v-model="selectedDistrict"></u-picker> </view> </template> <script> import { UPicker } from 'uview-ui'; export default { components: { UPicker, }, data() { return { provinceList: ['省1', '省2', '省3'], // 省份数据 cityList: [], // 城市数据,根据选择的省份动态生成 districtList: [], // 区域数据,根据选择城市动态生成 selectedProvince: '', // 已选择的省份 selectedCity: '', // 已选择城市 selectedDistrict: '', // 已选择的区域 }; }, methods: { handleProvinceChange(value) { // 根据已选择的省份 value,动态生成城市数据 // 可以通过接口请求获取对应省份下的城市数据 // 更新 cityList }, handleCityChange(value) { // 根据已选择城市 value,动态生成区域数据 // 可以通过接口请求获取对应城市下的区域数据 // 更新 districtList }, }, }; </script> ``` 在上述示例中,我们使用了三个 `u-picker` 组件实现省市区三级联动。`provinceList` 是省份的数据源,`cityList` 和 `districtList` 是在选择省份和城市时动态生成的数据源。通过监听 `change` 事件,我们可以在选择省份和城市时更新相应的数据源。 注意:上述示例中的数据源只是示意,实际开发中需要根据项目需求使用真实的数据源。 希望以上示例对你有所帮助!如果有任何疑问,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值