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
<template>
<div class="address">
<section style="width:100%; height: 100%;">
<el-form :model='form' class='demo-ruleForm' ref='form' label-width="100px" :rules='rules'>
<el-row :gutter='20'>
<el-col :span='20'>
<el-form-item prop='name' label="收货人">
<el-input v-model="form.name"></el-input>
</el-form-item>
</el-col>
</el-row>
<el-row :gutter='20'>
<el-col :span='20'>
<el-form-item prop='mobile' label="手机号">
<el-input v-model="form.mobile"></el-input>
</el-form-item>
</el-col>
</el-row>
<el-row :gutter='20' class='address'>
<el-col :span='20'>
<el-form-item prop='province' label="省">
<el-select v-model='form.province' placeholder='请选择省' @change='proChange'>
<el-option v-for='item in provinces' :key='item.value' :value='item.value' :label="item.label">
</el-option>
</el-select>
</el-form-item>
</el-col>
</el-row>
<el-row :gutter='20' class='address'>
<el-col :span='20'>
<el-form-item prop='city' label="市">
<el-select v-model='form.city' placeholder='请选择市' @change='cityChange'>
<el-option v-for='item in cities' :key='item.value' :value='item.value' :label="item.label">
</el-option>
</el-select>
</el-form-item>
</el-col>
</el-row>
<el-row :gutter='20' class='address'>
<el-col :span='20'>
<el-form-item prop='district' label="县/区">
<el-select v-model='form.district' placeholder='请选择区/县' @change='districtChange'>
<el-option v-for='item in districts' :key='item.value' :value='item.value' :label="item.label">
</el-option>
</el-select>
</el-form-item>
</el-col>
</el-row>
<el-row :gutter='20' class='address'>
<el-col :span='20'>
<el-form-item prop='detail' label="详细地址">
<el-input placeholder='请填写详细地址' :number='true' v-model='form.detail'>
</el-input>
</el-form-item>
</el-col>
</el-row>
</el-form>
</section>
</div>
</template>
<script>
import Address from './addr.js'
let type = 1
// let provinceValue = 2
// let cityValue = 52
//根据apid查找对象
function findcity(item) {
return item.type == type;
}
function findChildren(list, value) {
let filtered = list.filter((item) => {
return item.value === value
})
return filtered[0].children
}
//筛选出各省级对象
let pObj = Address.filter(findcity)
export default {
name: 'vueAddress',
props: ['province', 'city', 'district', 'detail', 'name', 'mobile'],
created() {
},
mounted() {
let vm = this
vm.show = vm.showAddressPicker
},
computed: {
cities: function () {
if(this.form.province){
return findChildren(pObj, this.form.province)
} else {
return []
}
},
districts: function () {
if(this.form.city){
return findChildren(this.cities, this.form.city)
} else {
return []
}
}
},
data() {
return {
rules: {
province: [{ required: true, message: '请选择省份', trigger: 'change' }],
city: [{ required: true, message: '请选择城市', trigger: 'change' }],
district: [{ required: true, message: '请选择区/县', trigger: 'change' }],
detail: [{ required: true, message: '请填写详细地址', trigger: 'change' }],
name: [{ required: true, message: '请填写收货人', trigger: 'change' }],
mobile: [{ required: true, message: '请填写手机号', trigger: 'change' }]
},
form: {
province: this.province,
city: this.city,
district: this.district,
detail: this.detail,
name: this.name,
mobile: this.mobile
},
provinces: pObj
}
},
watch: {
form: {
handler: function (val) {
this.$emit('change', val);
},
deep: true
}
},
methods: {
proChange: function (val, oldVal) {
if (oldVal) {
this.form.city = '';
this.form.district = '';
}
this.form.city = this.cities[0].value;
this.form.district = this.districts[0].value;
},
cityChange: function (val, oldVal) {
this.form.district = this.districts[0].value;
},
districtChange: function (val, oldVal) {
if (oldVal) {
this.form.detail = '';
}
},
detailChange: function (val) {
console.log(val);
}
}
}
</script>
<style scoped>
.mint-popup {
width: 100%;
}
.footer-btn {
box-sizing: border-box;
position: fixed;
width: 100%;
z-index: 2;
left: 0;
bottom: 3rem;
}
.input {
border: none;
}
.padding {
padding: 1.5rem;
font-size: 1.4rem;
}
.float-right {
float: right;
}
</style>