vue封装的通用五级地址联动选择器组件 [拿来即用]

数据来源:https://api.muxiaoguo.cn/doc/cityLinkage.html
UI依赖:https://element.eleme.cn/#/zh-CN/component/select
CSS依赖:http://lesscss.cn/

效果:

调用:

组件源码在最下面

清除验证:

提交数据

得到的返回值:

组件配置说明:

属性 说明 必传 类型 默认
labelWidth 表单的label宽度 Number 95
showRemark 是否需要地址的备注 Boolean false
col 表单的一个输入组宽度,按照elemenUI的占位 Number 6
data 回显的数据 回显时必传 Object {}

回显数据规则:

必须是对应:

1
2
3
4
5
6
7
8
9
10
11
{
provinces: "",
city: "",
district: "",
towns: "",
village: "",
buildNo: "",
unitNo: "",
houseNo: "",
remark: ""
}

组件源码

拿来即用

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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
 <template>
<el-form
:model="forms"
:label-width="labelWidth + 'px'"
:rules="rules"
ref="forms"
>
<el-row :gutter="10">
<el-col :span="col">
<el-form-item
:label="labelWidth === 0 ? '' : '省/自治区/直辖市'"
prop="provinces"
>
<el-select
clearable
filterable
v-model="forms.provinces"
@change="selection('1', $event,'0')"
:placeholder="labelWidth === 0 ? '省/自治区' : ''"
>
<el-option
v-for="item in dropdowns.provinces"
:key="item.code"
:label="item.name"
:value="item.code"
></el-option>
</el-select>
</el-form-item>
</el-col>
<el-col :span="col">
<el-form-item :label="labelWidth === 0 ? '' : '市/区'" prop="city">
<el-select
clearable
filterable
v-model="forms.city"
no-data-text="请选择省/自治区"
@change="selection('2', $event,'0')"
:placeholder="labelWidth === 0 ? '市/区' : ''"
>
<el-option
v-for="item in dropdowns.city"
:key="item.code"
:label="item.name"
:value="item.code"
></el-option>
</el-select>
</el-form-item>
</el-col>
<el-col :span="col">
<el-form-item :label="labelWidth === 0 ? '' : '区/县'" prop="district">
<el-select
clearable
filterable
v-model="forms.district"
no-data-text="请选择市/区"
@change="selection('3', $event,'0')"
:placeholder="labelWidth === 0 ? '区/县' : ''"
>
<el-option
v-for="item in dropdowns.district"
:key="item.code"
:label="item.name"
:value="item.code"
></el-option>
</el-select>
</el-form-item>
</el-col>
<el-col :span="col">
<el-form-item :label="labelWidth === 0 ? '' : '乡镇/街道'" prop="towns">
<el-select
clearable
filterable
v-model="forms.towns"
no-data-text="请选择区/县"
@change="selection('4', $event,'0')"
:placeholder="labelWidth === 0 ? '乡镇/街道' : ''"
>
<el-option
v-for="item in dropdowns.towns"
:key="item.code"
:label="item.name"
:value="item.code"
></el-option>
</el-select>
</el-form-item>
</el-col>
<el-col :span="col">
<el-form-item
:label="labelWidth === 0 ? '' : '村/会/庄'"
prop="village"
>
<el-select
clearable
filterable
v-model="forms.village"
placeholder=""
>
<el-option
v-for="item in dropdowns.village"
:key="item.code"
:label="item.name"
:value="item.code"
></el-option>
</el-select>
</el-form-item>
</el-col>
<el-col :span="col">
<el-form-item
class="specific"
:label="labelWidth === 0 ? '' : '具体地址'"
>
<el-input
clearable
v-model.number="forms.buildNo"
:placeholder="labelWidth === 0 ? '楼号' : ''"
>
<template v-if="labelWidth > 0" slot="append">号楼</template>
</el-input>
<el-input
clearable
v-model.number="forms.unitNo"
:placeholder="labelWidth === 0 ? '单元' : ''"
>
<template v-if="labelWidth > 0" slot="append"
>单元</template
></el-input
>
<el-input
clearable
v-model.number="forms.houseNo"
:placeholder="labelWidth === 0 ? '门牌号' : ''"
>
<template v-if="labelWidth > 0" slot="append"
>室</template
></el-input
>
</el-form-item>
</el-col>
<el-col v-if="showRemark" :span="col">
<el-form-item :label="labelWidth === 0 ? '' : '地址备注'">
<el-input
v-model="forms.remark"
:placeholder="labelWidth === 0 ? '地址备注' : ''"
></el-input>
</el-form-item>
</el-col>
</el-row>
</el-form>
</template>

<script>
/**
* @author 全易
* @time 2021-08-13 17:12:59 星期五
* @description 地址选择器
*/

export default {
name: "es-address",
props: {
labelWidth: {
type: Number,
default: 95,
},
showRemark: {
type: Boolean,
default: false,
},
col: {
type: Number,
default: 6,
},
data: {
type: Object,
default() {
return {};
},
},
},
watch: {
data(now, old) {
// console.log(now);
this.forms = now;

for (let i = 1; i < 4; i++) {
// i必须转字符串
switch (i) {
case 1:
this.selection(i + "", now.provinces);
break;
case 2:
this.selection(i + "", now.city);
break;
case 3:
this.selection(i + "", now.district);
break;
case 4:
this.selection(i + "", now.village);
break;
}
}
},
},
data() {
return {
dropdowns: {
provinces: [],
city: [],
district: [],
towns: [],
village: [],
},
rules: {
provinces: [
{
required: true,
message: "请选择省/自治区/直辖市",
trigger: "change",
},
],
city: [{ required: true, message: "请选择市/区", trigger: "change" }],
district: [
{ required: true, message: "请选择区/县", trigger: "change" },
],
towns: [
{ required: true, message: "请选择乡镇/街道", trigger: "change" },
],
village: [
{ required: true, message: "请选择村/会/庄", trigger: "change" },
],
},
forms: {
provinces: "",
city: "",
district: "",
towns: "",
village: "",
buildNo: "",
unitNo: "",
houseNo: "",
remark: "",
},
};
},
created() {
this.selection("0", "getCity");
},
methods: {
selection(level, code) {
// console.log(level, code);
fetch(`https://api.muxiaoguo.cn/api/cityLinkage?id=${code}`) // 如果不能用了,就自己找个接口吧
.then((response) => {
return response.json();
})
.then((result) => {
// console.log(result);
switch (level) {
case "0":
this.dropdowns.provinces = result.data;
break;
case "1":
this.dropdowns.city = result.data;

this.dropdowns.district = [];
this.dropdowns.towns = [];
this.dropdowns.village = [];
this.forms.city = "";
this.forms.district = "";
this.forms.towns = "";
this.forms.village = "";

break;
case "2":
this.dropdowns.district = result.data;

this.dropdowns.towns = [];
this.dropdowns.village = [];
this.forms.district = "";
this.forms.towns = "";
this.forms.village = "";

break;
case "3":
this.dropdowns.towns = result.data;

this.dropdowns.village = [];
this.forms.towns = "";
this.forms.village = "";

break;
case "4":
this.dropdowns.village = result.data;

this.forms.village = "";

break;
}
});
},
submit() {
this.$refs["forms"].validate((valid) => {
if (valid) {
this.$emit("submit", this.forms);
}
});
},
resetFields() {
this.$refs["forms"].resetFields();
for (let i in this.forms) {
this.forms[i] = "";
}
},
},
};
</script>

<style lang="less" scoped>
/deep/.specific {
.el-form-item__content {
display: grid;
grid-template-columns: auto auto auto;
gap: 10px;
justify-content: space-between;
&::after,
&::before {
display: none;
}
}
}
</style>

地址数据来源:

https://github.com/modood/Administrative-divisions-of-China
https://gitee.com/modood/Administrative-divisions-of-China

三级一体选择器

使用elementUI的Cascader级联选择器进行地址三级一体选择
https://blog.csdn.net/qq_42618566/article/details/115403544


vue封装的通用五级地址联动选择器组件 [拿来即用]
https://github.com/chergn/chergn.github.io/4a836d2b118d/
作者
全易
发布于
2024年3月28日
许可协议