ThreeLevelRegionSelector.vue
3.95 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
<template>
<div class="three-level-region-selector">
<el-row :gutter="10">
<el-col :span="8">
<el-select
v-model="selectedProvince"
placeholder="选择省份"
clearable
@change="handleProvinceChange"
style="width: 100%;"
>
<el-option
v-for="province in provinces"
:key="province"
:label="province"
:value="province"
></el-option>
</el-select>
</el-col>
<el-col :span="8">
<el-select
v-model="selectedCity"
placeholder="选择地市"
clearable
@change="handleCityChange"
:disabled="!selectedProvince"
style="width: 100%;"
>
<el-option
v-for="city in cities"
:key="city"
:label="city"
:value="city"
></el-option>
</el-select>
</el-col>
<el-col :span="8">
<el-select
v-model="selectedDistrict"
placeholder="选择区县"
clearable
:disabled="!selectedCity"
style="width: 100%;"
>
<el-option
v-for="district in districts"
:key="district"
:label="district"
:value="district"
></el-option>
</el-select>
</el-col>
</el-row>
</div>
</template>
<script>
// 地区数据
const regionData = {
'江苏省': {
'南京市': ['玄武区', '秦淮区', '建邺区', '鼓楼区', '浦口区', '栖霞区', '雨花台区', '江宁区', '六合区', '溧水区', '高淳区'],
'南通市': ['崇川区', '港闸区'],
'苏州市': ['姑苏区', '虎丘区']
}
}
export default {
name: 'ThreeLevelRegionSelector',
props: {
value: {
type: Object,
default: () => ({
province: '',
city: '',
district: ''
})
},
placeholder: {
type: String,
default: '请选择区域'
}
},
data() {
return {
selectedProvince: this.value.province,
selectedCity: this.value.city,
selectedDistrict: this.value.district
}
},
computed: {
provinces() {
return Object.keys(regionData)
},
cities() {
if (!this.selectedProvince) return []
return Object.keys(regionData[this.selectedProvince])
},
districts() {
if (!this.selectedProvince || !this.selectedCity) return []
return regionData[this.selectedProvince][this.selectedCity]
}
},
watch: {
value: {
handler(newVal) {
this.selectedProvince = newVal.province || ''
this.selectedCity = newVal.city || ''
this.selectedDistrict = newVal.district || ''
},
deep: true
},
selectedProvince() {
if (!this.provinces.includes(this.selectedProvince)) {
this.selectedCity = ''
this.selectedDistrict = ''
}
this.emitChange()
},
selectedCity() {
if (!this.cities.includes(this.selectedCity)) {
this.selectedDistrict = ''
}
this.emitChange()
},
selectedDistrict() {
this.emitChange()
}
},
methods: {
handleProvinceChange() {
if (!this.provinces.includes(this.selectedProvince)) {
this.selectedCity = ''
this.selectedDistrict = ''
}
this.emitChange()
},
handleCityChange() {
if (!this.cities.includes(this.selectedCity)) {
this.selectedDistrict = ''
}
this.emitChange()
},
emitChange() {
this.$emit('input', {
province: this.selectedProvince,
city: this.selectedCity,
district: this.selectedDistrict
})
this.$emit('change', {
province: this.selectedProvince,
city: this.selectedCity,
district: this.selectedDistrict
})
}
}
}
</script>
<style lang="scss" scoped>
.three-level-region-selector {
.el-select {
::v-deep .el-input__inner {
border-radius: 4px;
}
}
}
</style>