AreaSelect.vue
6.96 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<template>
<el-form-item :label="label"
:prop="prop">
<el-select v-model="provinceId"
filterable
placeholder="请选择"
size="small"
class="el-slect"
:disabled="disabled"
@change="povinceChange">
<el-option v-for="item in provinceOpts"
:key="item.id"
:label="item.name"
:value="item.id" />
</el-select>
<el-select v-show="!isHideCity"
v-model="cityId"
placeholder=""
size="small"
class="el-slect"
:disabled="disabled"
@change="cityChange">
<el-option v-for="item in cityOpts"
:key="item.id"
:label="item.name"
:value="item.id" />
</el-select>
<el-select v-show="!isHideArea"
v-model="areaId"
placeholder=""
size="small"
class="el-slect"
:disabled="disabled"
@change="areaChange">
<el-option v-for="item in areaOpts"
:key="item.id"
:label="item.name"
:value="item.id" />
</el-select>
</el-form-item>
</template>
<script>
// import chinaData from './china'
export default {
props: ['label', 'prop', 'id', 'init', 'disabled', 'fixArea'],
data() {
// const provinceOptions = chinaData.province
// const slectedProvince = provinceOptions[0]
return {
chinaData: '',
province: '',
provinceId: '',
// provinceOpts: provinceOptions,
provinceOpts: [],
city: '',
cityId: '',
cityOpts: [],
isHideCity: true,
isHideArea: true,
area: '',
areaId: '',
areaOpts: []
}
},
computed: {
areaStr: {
get() {
return this.province + this.city + this.area
}
},
areaStrSplit: {
get() {
let str = this.province
if (this.city) {
str += `,${this.city}`
}
if (this.area) {
str += `,${this.area}`
}
return str
}
}
},
watch: {
id(newVal) {
if (this.init) {
if (newVal) {
console.log('newVal', newVal)
console.log('fixArea', this.fixArea)
console.log('area', this.chinaData.area)
let cityId = '' // 地区所属城市id
let provinceId = '' // 城市所属省份id
// 根据区域id反查城市id
for (const cityKey in this.chinaData.area) {
const areaInfo = this.chinaData.area[cityKey]
if (areaInfo.find((item) => item.id === newVal)) {
console.log('cityKey', cityKey)
cityId = cityKey
break
}
}
// 当城市id没找到时代表该城市下没有区域信息,因此区域id存储时存的是上一级的城市id,将获取的区域id赋值给城市id去查找省份
if (!cityId) {
cityId = newVal
}
// 根据城市id反查省份id
for (const provinceKey in this.chinaData.city) {
const cityInfo = this.chinaData.city[provinceKey]
if (cityInfo.find((item) => item.id === cityId)) {
console.log('provinceKey', provinceKey)
provinceId = provinceKey
break
}
}
// const s1 = newVal.substr(0, 2) + '0000'
// const s2 = newVal.substr(0, 4) + '00'
// this.provinceId = s1
this.provinceId = provinceId
this.povinceChange(this.provinceId)
// this.cityId = s2
this.cityId = cityId
this.pickCity(this.cityId)
this.areaId = newVal
} else {
this.provinceId = ''
this.cityId = ''
this.areaId = ''
this.isHideCity = true
this.isHideArea = true
}
this.$emit('areaChangeInit')
}
}
},
mounted() {
this.getArea()
},
methods: {
getArea() {
const url = new URL('https:/img01.glsx.com.cn/js/area.json')
fetch(url)
// fetch('area.json')
.then(response => response.json())
.then(data => {
console.log(data)
this.chinaData = data
this.provinceOpts = this.chinaData.province
})
.catch(error => {
console.error('Error:', error)
})
},
povinceChange(provinceId) {
console.log(provinceId, 'provinceId') // this.$emit('onChangArea'
this.provinceId = provinceId
// const cityOptions = chinaData.city[provinceId]
const cityOptions = this.chinaData.city[provinceId]
if (cityOptions) {
const selectedCity = cityOptions[0]
this.province = selectedCity.province
this.cityId = selectedCity.id
this.city = selectedCity.name
this.cityOpts = cityOptions
this.cityChange()
if (selectedCity.name == '市辖区') {
this.isHideCity = true
} else {
this.isHideCity = false
}
} else {
this.isHideCity = true
this.isHideArea = true
// const provinceItem = chinaData.province.find((item) => item.id === provinceId)
const provinceItem = this.chinaData.province.find((item) => item.id === provinceId)
this.province = provinceItem.name
this.city = ''
this.area = ''
this.$emit('onAreaSelectChange', this.areaStr, this.provinceId, this.areaStrSplit)
}
},
pickCity(cityId) {
this.cityId = cityId
// const areaOptions = chinaData.area[cityId]
const areaOptions = this.chinaData.area[cityId]
if (areaOptions.length) {
if (areaOptions[0].name == '市辖区') {
areaOptions.splice(0, 1)
}
this.areaOpts = areaOptions
const selectedArea = areaOptions[0]
this.city = selectedArea.city
this.areaId = this.id || selectedArea.id
this.area = selectedArea.name
this.isHideArea = false
this.$emit('onAreaSelectChange', this.areaStr, this.areaId, this.areaStrSplit)
} else {
this.isHideArea = true
// const cityItem = chinaData.city[this.provinceId].find((item) => item.id === cityId)
const cityItem = this.chinaData.city[this.provinceId].find((item) => item.id === cityId)
this.city = cityItem.name
this.area = ''
this.areaId = ''
this.$emit('onAreaSelectChange', this.areaStr, this.cityId, this.areaStrSplit)
}
},
cityChange() {
this.pickCity(this.cityId)
},
areaChange() {
// const areaOptions = chinaData.area[this.cityId]
const areaOptions = this.chinaData.area[this.cityId]
const selectedArea = areaOptions.find((o) => {
return o.id == this.areaId
})
this.area = selectedArea.name
this.$emit('onAreaSelectChange', this.areaStr, this.areaId, this.areaStrSplit)
}
}
}
</script>
<style scoped>
.el-select {
width: 120px;
}
</style>