<van-field
readonly
clickable
name="picker"
:value="value"
label="省市區"
placeholder="選擇省市區"
@click="showPicker = true"
/>
data() {
return {
value: "",
showPicker: false,
columns: [],
area: "",
shengId: "",
shiId: "",
quId: ""
};
methods: {
//獲取id,以及展示信息
onConfirms(values, index) {
this.value = values.toString();
this.showPicker = false;
this.arrayid = index;
for (let a = 0; a < this.area.length; a++) {
if (a == this.arrayid[0]) {
//獲取省級id
this.shengId = this.area[a].id;
console.log(this.shengId);
}
}
// 市級
let city = this.area[this.arrayid[0]].children;
for (let b = 0; b < city.length; b++) {
if (b == this.arrayid[1]) {
this.shiId = city[b].id;
console.log(this.shiId);
}
}
// 縣級
let county = this.area[this.arrayid[0]].children[this.arrayid[1]];
for (let c = 0; c < county.children.length; c++) {
if (c == this.arrayid[2]) {
this.quId = county.children[c].id;
console.log(this.quId);
}
}
},
//獲取所有的地區信息
getaddress() {
this.$axios
.post(
"省市區接口地址"
)
.then(res => {
console.log(res);
if (res.data.code === 100000) {
this.area = res.data.result;
for (let index = 0; index < res.data.result.length; index++) {
let sheng = res.data.result[index];
sheng.text = res.data.result[index].name;
sheng.children = res.data.result[index].city_info;
this.columns.push(sheng);
// 循環市級單位
for (let i = 0; i < res.data.result[index].children.length; i++) {
let shi = res.data.result[index].children[i];
shi.text = shi.name;
shi.children = res.data.result[index].children[i].county_info;
//循環縣級
for (
let j = 0;
j < res.data.result[index].children[i].children.length;
j++
) {
let xian = res.data.result[index].children[i].children[j];
xian.text = xian.name;
}
}
}
console.log(this.columns);
}
});
},
}
由於后台給的接口字段和vant ui 官方的不同,所以手動插了幾個值,text ,children,比較麻煩,