<template>
<view>
<view class="flex-row align-center" @click="set_picker()">
<view class="flex-grow">{{ value }}</view>
<view class="align-center">
<u-icon color="#999797" size="14" name="arrow-right"></u-icon>
</view>
</view>
<u-picker
:closeOnClickOverlay="true"
:show="picker_show"
:immediateChange="true"
:defaultIndex="picker_index"
ref="uPicker"
title="所在地区"
:columns="columns"
keyName="regionName"
@confirm="confirm"
@change="changeHandler"
@close="picker_show = false"
@cancel="picker_show = false"
>
</u-picker>
</view>
</template>
<script>
import Area from "../../common/area.js";
export default {
data() {
return {
picker_show: false,
columns: [[], [], []],
picker_value: "",
picker_index: [],
pramas: {
province: "",
city: "",
area: "",
province_dictText: "",
city_dictText: "",
area_dictText: "",
picker_value: "",
picker_idx: [],
},
province: [],
city: [],
county:[],
};
},
props: {
apiData: {
type: [String, Object],
default: "",
},
block: {
type: Boolean,
default: true,
},
info: {
type: Boolean,
default: false,
},
value: {
type: String,
default: "",
},
address_data: {
type: [String, Object],
default: () => {},
},
edit_btn: {
type: Boolean,
default: false,
},
},
methods: {
async set_picker() {
var idx = this.picker_index;
this.picker_show = true;
console.log(idx);
const picker = this.$refs.uPicker;
console.log("picker", picker);
if (idx.length == 0) {
picker.setColumnValues(0, this.province);
await this.get_city_list(this.province[0]);
picker.setColumnValues(1, this.city);
await this.get_county_list(this.city[0])
picker.setColumnValues(2, this.county);
}
this.$emit("on-show", this.picker_show);
},
confirm(e) {
this.pramas.province = e.value[0].regionCode;
this.pramas.city = e.value[1].regionCode;
this.pramas.area = e.value[2].regionCode;
this.pramas.province_dictText = e.value[0].regionName;
this.pramas.city_dictText = e.value[1].regionName;
this.pramas.area_dictText = e.value[2].regionName;
this.pramas.picker_idx = e.indexs;
this.picker_index = e.indexs;
this.pramas.picker_value =
this.pramas.province_dictText +
this.pramas.city_dictText +
this.pramas.area_dictText;
this.picker_show = false;
this.$emit("on-confirm", this.pramas);
},
async changeHandler(e) {
const {
columnIndex,
value,
values,
index,
indexs,
picker = this.$refs.uPicker,
} = e;
if (columnIndex === 0) {
console.log("第1联级", e);
await this.get_city_list(this.province[e.indexs[0]]);
picker.setColumnValues(1, this.city);
this.get_county_list(this.city[0])
picker.setColumnValues(2, this.county);
}
if (columnIndex === 1) {
console.log("第2联级", e);
await this.get_county_list(this.city[e.indexs[1]])
picker.setColumnValues(2,this.county);
}
this.$emit("on-change");
},
get_province_list() {
let province_list = [];
for (let x in Area.province_list) {
let item = {
regionCode: x,
regionName: Area.province_list[x],
level: 1,
children: [],
};
province_list.push(item);
}
this.province = province_list;
},
get_city_list(pramas) {
console.log("get_city_list----pramas->", pramas);
let city_list = [];
for (let y in Area.city_list) {
if (y.substr(0, 2) == pramas.regionCode.substr(0, 2)) {
let item = {
regionCode: y,
regionName: Area.city_list[y],
level: 2,
children: [],
};
city_list.push(item);
}
}
this.city = city_list;
},
get_county_list(pramas) {
console.log("get_county_list----pramas->", pramas);
let county_list = [];
for (let z in Area.county_list) {
if (z.substr(0, 4) == pramas.regionCode.substr(0, 4)) {
let item = {
regionCode: z,
regionName: Area.county_list[z],
level: 3,
children: [],
};
county_list.push(item);
}
}
this.county = county_list;
},
},
mounted() {
this.get_province_list();
},
};
</script>
<style scoped>
.info {
color: #c4c4d6;
}
</style>