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

117 lines
2.5 KiB

3 years ago
<!--
* @Author: ch
* @Date: 2022-03-29 16:05:06
* @LastEditors: ch
* @LastEditTime: 2022-05-21 20:04: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
ref="picker"
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.json';
3 years ago
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];
}
},
3 years ago
changeCity(val) {
if(val.columnIndex == 2){
return;
}
let newIndexs = val.indexs;
if(val.columnIndex == 0){
newIndexs[1] = 0;
3 years ago
}
newIndexs[2] = 0;
3 years ago
this.defaultIndex = newIndexs;
3 years ago
const city = val.value[0].children;
3 years ago
const area = city[newIndexs[1]].children;
this.$refs.picker.setColumnValues(1, city);
this.$refs.picker.setColumnValues(2, 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>