index.js
1.92 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
70
71
72
73
Component({
/**
* 组件的属性列表
*/
properties: {
provinceShow: Boolean,
provinceList: {
type: Array,
observer: function (val) {
console.log(val, '++++');
this.setData({
provinceList: val
})
}
}
},
/**
* 组件的初始数据
*/
data: {
region: [0, 0, 0], // 地址每列的index
regionCode: [],
provinceList: []
},
/**
* 组件的方法列表
*/
methods: {
// emitHideRegion(){},
// 省市切换
bindChange(e) {
// 用于对比滑动的是哪一列数据
const val = e.detail.value
if (this.data.region[0] !== val[0]) {
// 如果滑动的是第一列数据,让二三列恢复到0
this.setData({
region: [val[0], 0, 0]
})
} else if (this.data.region[1] !== val[1]) {
// 滑动的是第二列数据
this.setData({
region: [val[0], val[1], 0]
})
} else {
this.setData({
region: e.detail.value
})
}
},
// 确定提交
comfirm() {
const {provinceList, region} = this.data
// 省市code
const provinceCode = provinceList[region[0]].value
const cityCode = provinceList[region[0]].children[region[1]].value
const townCode = provinceList[region[0]].children[region[1]].children[region[2]].value
const codeData = [provinceCode, cityCode, townCode]
// 省市名称
const provinceName = provinceList[region[0]].label
const cityName = provinceList[region[0]].children[region[1]].label
const townName = provinceList[region[0]].children[region[1]].children[region[2]].label
const provinceNameData = [provinceName, cityName, townName]
this.triggerEvent('getProvinceCode', {codeData, provinceNameData})
this.triggerEvent('cancelProvince')
},
// 取消
cancel() {
this.triggerEvent('cancelProvince')
}
}
})