修复选择地址回显的问题

This commit is contained in:
Gao xiaosong
2021-11-07 23:51:59 +08:00
parent 0a13a11ec6
commit 5e8f7befd9
3 changed files with 201 additions and 164 deletions

View File

@ -1,6 +1,6 @@
<template>
<view>
<text class="uni-input" @tap="open">{{value}}</text>
<text class="uni-input" @tap="open">{{ value }}</text>
<uni-popup ref="popup" type="bottom">
<view class="cityselect">
<view class="cityselect-header">
@ -9,13 +9,13 @@
</view>
<view class="cityselect-nav">
<view class="item" v-if="provinceActive" @tap="changeNav(0)">
<text>{{provinceActive.n}}</text>
<text>{{ provinceActive.n }}</text>
</view>
<view class="item" v-if="cityActive" @tap="changeNav(1)">
<text>{{cityActive.n}}</text>
<text>{{ cityActive.n }}</text>
</view>
<view class="item" v-if="districtActive" @tap="changeNav(2)">
<text>{{districtActive.n}}</text>
<text>{{ districtActive.n }}</text>
</view>
<view class="item active" v-else>
<text>请选择</text>
@ -27,14 +27,9 @@
<swiper-item>
<scroll-view scroll-y class="cityScroll">
<view>
<view
class="cityselect-item"
v-for="(item,index) in province"
:key="index"
@tap="selectProvince(index)"
>
<view class="cityselect-item-box">
<text>{{item.n}}</text>
<view class="cityselect-item" v-for="(item, index) in province" :key="index" @tap="selectProvince(index)">
<view class="cityselect-item-box" :class="{ active: isProvinceActive(item) }">
<text>{{ item.n }}</text>
</view>
</view>
</view>
@ -43,14 +38,9 @@
<swiper-item>
<scroll-view scroll-y class="cityScroll">
<view>
<view
class="cityselect-item"
v-for="(item,index) in city"
:key="index"
@tap="selectCity(index)"
>
<view class="cityselect-item-box">
<text>{{item.n}}</text>
<view class="cityselect-item" v-for="(item, index) in city" :key="index" @tap="selectCity(index)">
<view class="cityselect-item-box" :class="{ active: isCityActive(item)}">
<text>{{ item.n }}</text>
</view>
</view>
</view>
@ -59,14 +49,9 @@
<swiper-item>
<scroll-view scroll-y class="cityScroll">
<view>
<view
class="cityselect-item"
v-for="(item,index) in district"
:key="index"
@tap="selectDistrict(index)"
>
<view class="cityselect-item-box">
<text>{{item.n}}</text>
<view class="cityselect-item" v-for="(item, index) in district" :key="index" @tap="selectDistrict(index)">
<view class="cityselect-item-box" :class="{ active: isDistrictActive(item) }">
<text>{{ item.n }}</text>
</view>
</view>
</view>
@ -80,21 +65,21 @@
</template>
<script type="text/babel">
import uniPopup from "./uni-popup/uni-popup.vue";
import uniPopupMessage from "./uni-popup/uni-popup-message.vue";
import uniPopupDialog from "./uni-popup/uni-popup-dialog.vue";
import uniPopup from './uni-popup/uni-popup.vue'
import uniPopupMessage from './uni-popup/uni-popup-message.vue'
import uniPopupDialog from './uni-popup/uni-popup-dialog.vue'
export default {
name: "CitySelect",
name: 'CitySelect',
components: {
uniPopup,
uniPopupMessage,
uniPopupDialog
uniPopupDialog,
},
props: ["callback", "items", "defaultValue"],
props: ['callback', 'items', 'defaultValue'],
data() {
return {
value: "请选择",
value: '请选择',
show: this.value,
province: [],
provinceActive: null,
@ -102,60 +87,107 @@ export default {
cityActive: null,
district: [],
districtActive: null,
current: 0
};
current: 0,
}
},
watch: {
items(nextItem) {
this.province = nextItem;
items(next) {
this.province = next
},
defaultValue(next) {
this.value = `${next.province.n} ${next.city.n} ${next.district.n}`
this.setDefaultValue(this.items, next)
},
defaultValue(next){
this.value=next
}
},
mounted() {
console.log(this);
console.log(this)
if (this.value) {
this.value = this.value;
this.value = this.value
}
this.province = this.items;
this.province = this.items
},
methods: {
isProvinceActive(item) {
return this.provinceActive && item.v == this.provinceActive.v
},
isCityActive(item) {
return this.cityActive && item.v == this.cityActive.v
},
isDistrictActive(item) {
return this.districtActive && item.v == this.districtActive.v
},
setDefaultValue(items, value) {
if (!items || !value) {
return
}
this.province = items
items.map(province => {
if (province.n == value.province.n) {
this.city = province.c
this.provinceActive = {
v: province.v,
n: value.province.n,
}
province.c.map(city => {
if (city.n == value.city.n) {
this.district = city.c
this.cityActive = {
v: city.v,
n: value.city.n,
}
city.c.map(district => {
if (district.n == value.district.n) {
this.districtActive = {
v: city.v,
n: value.district.n,
}
}
})
}
})
}
})
console.log(this.provinceActive, this.cityActive, this.districtActive)
console.log(this)
},
open() {
this.province = this.items;
this.provinceActive = null;
this.cityActive = null;
this.districtActive = null;
this.city = [];
this.district = [];
this.current = 0;
this.$refs.popup.open();
this.province = this.items
this.provinceActive = null
this.cityActive = null
this.districtActive = null
this.city = []
this.district = []
this.current = 0
this.$refs.popup.open()
this.setDefaultValue(this.items, this.defaultValue)
},
changeNav(index) {
if (index == 0) {
this.provinceActive = null;
this.provinceActive = null
}
if (index == 1) {
this.cityActive = null;
this.cityActive = null
}
if (index == 2) {
this.districtActive = null;
this.districtActive = null
}
this.current = index;
this.current = index
},
selectProvince(index) {
this.provinceActive = this.province[index];
this.city = this.province[index].c;
this.current = 1;
this.provinceActive = this.province[index]
this.city = this.province[index].c
this.current = 1
},
selectCity(index) {
this.cityActive = this.city[index];
this.district = this.city[index].c;
this.current = 2;
this.cityActive = this.city[index]
this.district = this.city[index].c
this.current = 2
},
selectDistrict(index) {
this.districtActive = this.district[index];
this.value = `${this.provinceActive.n} ${this.cityActive.n} ${this.districtActive.n}`;
this.districtActive = this.district[index]
this.value = `${this.provinceActive.n} ${this.cityActive.n} ${this.districtActive.n}`
// this.callback({
// province: {
// id: this.provinceActive.v,
@ -170,24 +202,24 @@ export default {
// name: this.districtActive.n
// }
// });
this.$emit("callback", {
this.$emit('callback', {
province: {
id: this.provinceActive.v,
name: this.provinceActive.n
name: this.provinceActive.n,
},
city: {
id: this.cityActive.v,
name: this.cityActive.n
name: this.cityActive.n,
},
district: {
id: this.districtActive.v,
name: this.districtActive.n
}
});
this.$refs.popup.close();
}
}
};
name: this.districtActive.n,
},
})
this.$refs.popup.close()
},
},
}
</script>
<style lang="less">
@ -197,14 +229,14 @@ export default {
background-color: #fff;
z-index: 1502;
position: relative;
padding-bottom: 0;
padding-bottom: constant(safe-area-inset-bottom);
padding-bottom: env(safe-area-inset-bottom);
padding-bottom: 0;
padding-bottom: constant(safe-area-inset-bottom);
padding-bottom: env(safe-area-inset-bottom);
.cityScroll {
height: 100%;
}
.swiper {
height: 800rpx;
height: 800rpx;
}
}
.cityselect-header {
@ -224,7 +256,7 @@ export default {
z-index: 0;
bottom: 0;
left: 0;
content: "";
content: '';
width: 100%;
background-image: linear-gradient(0deg, #ececec 50%, transparent 0);
}
@ -274,8 +306,11 @@ export default {
max-height: 65rpx;
font-size: 26rpx;
color: #333;
&.active{
color:#f23030 !important;
}
&:after {
content: "";
content: '';
height: 1rpx;
position: absolute;
z-index: 0;