You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
shop-app/components/BsSelectCity.vue

104 lines
2.2 KiB

3 years ago
<!--
* @Author: ch
* @Date: 2022-03-29 16:05:06
* @LastEditors: ch
3 years ago
* @LastEditTime: 2022-04-30 15:33:47
3 years ago
* @Description: file content
-->
<template>
<view>
<UiCell title="所在区域" @click="show = true">
<template slot="value">
3 years ago
<input class="cityInput" disabled :value="valueText" placeholder="请选择省市区" />
3 years ago
</template>
</UiCell>
<u-picker
:show="show"
3 years ago
keyName="name"
:columns="columnsData"
:defaultIndex="defaultIndex"
3 years ago
@cancel="show = false"
3 years ago
@change="changeCity"
@confirm="confirm"
3 years ago
/>
</view>
</template>
<script>
import UiCell from "./UiCell";
3 years ago
import Province from '@/common/dicts/area';
const DefaultCityColumn = Province[0].children;
const DefaultAreaColumn = DefaultCityColumn[0].children;
3 years ago
export default {
components: { UiCell },
3 years ago
props: {
value : {
type : Array,
default : []
}
},
3 years ago
data() {
return {
show: false,
3 years ago
columnsData : [],
defaultIndex : []
3 years ago
};
},
computed:{
valueText(){
3 years ago
return this.value.map(i => i.name).join('/');
3 years ago
},
},
3 years ago
watch:{
value(val){
this.setColumAndDefalut(val);
}
},
mounted(){
this.setColumAndDefalut(this.value);
3 years ago
3 years ago
},
3 years ago
methods: {
3 years ago
/**
* 设置列数据和默认选中值
*/
setColumAndDefalut(val){
if(val.length){
// 省选中值下标
const provinceIndex = Province.findIndex(item => val[0].code === item.code) || 0;
// 市选数据
const city = Province[provinceIndex].children;
const cityIndex = city.findIndex(item => val[1].code === item.code) || 0;
// 区数据
const area = city[cityIndex].children;
const areaIndex = area.findIndex(item => val[2].code === item.code) || 0;
this.columnsData = [Province, city, area];
this.defaultIndex = [provinceIndex, cityIndex, areaIndex]
}else{
this.columnsData = [Province, DefaultCityColumn, DefaultAreaColumn];
}
},
changeCity(val, b, c) {
const city = val.value[0].children;
const area = val.value[1].children;
this.columnsData = [Province, city, area]
3 years ago
},
confirm(value) {
3 years ago
this.show = false;
this.$emit('input', value.value)
this.$emit('change', value.value)
3 years ago
},
},
};
</script>
<style lang="scss" scoped>
.cityInput{
flex: 1;
font-size: $font-size-base;
3 years ago
padding-left: 40rpx;
}
</style>