areaCheck.vue
2.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<template>
<view class="">
<u-picker @close="close" :immediateChange ="true" :show="shows" ref="uPicker" :columns="columns" @confirm="confirm" @change="changeHandler"></u-picker>
</view>
</template>
<script>
import localData from '../utils/address-data.js'
export default {
props: {
shows: {
type:Boolean,
default:false
}
},
data() {
return {
columns: [],
columnData: [],
}
},
mounted() {
this.columns[0] = localData.map(item=>item.name)
this.columns[1] = localData[0].cities.map(item=>item.name)
this.columns[2] = localData[0].cities[0].districts.map(item=>item.name)
},
methods: {
close() {
this.$emit('close', false)
},
changeHandler(e) {
const {
columnIndex,
value,
values, // values为当前变化列的数组内容
index,
// 微信小程序无法将picker实例传出来,只能通过ref操作
picker = this.$refs.uPicker
} = e
// 当第一列值发生变化时,变化第二列(后一列)对应的选项
console.log('变化',index)
if (columnIndex === 0) {
// picker为选择器this实例,变化第二列对应的选项
let arr = localData.find(item=>item.name == value[0])
let arr1 = arr.cities.map(item=>item.name)
this.columnData = arr1
console.log('市',arr1[0],arr)
let distinguish = arr.cities[0].districts.map(item=>item.name)
picker.setColumnValues(1, arr1) //第二列
picker.setColumnValues(2, distinguish) //第三列
}
if (columnIndex === 1) {
// picker为选择器this实例,变化第三列对应的选项
let economize = localData.find(item=>item.name == value[0])
let city = economize.cities.find(item=>item.name == value[1])
let distinguish = city.districts.map(item=>item.name)
picker.setColumnValues(2, distinguish)
}
},
// 回调参数为包含columnIndex、value、values
confirm(e) {
this.$emit('popShow',e.value)
}
}
}
</script>
<style>
</style>