common.js
6.25 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
232
233
234
235
/**
* @authors Mzong(mzong121491@gmail.com)
* @date 2019-01-18 11:15
* @requires layui
* @version $1.1.0$
*/
layui.define("layer", function(exports){
//提示:模块也可以依赖其它模块,如:layui.define('layer', callback);
var $ = layui.jquery,
form = layui.form;
// 返回的对象
var returnObj = {
/**
* 省市县三级联动
*
* @param { string } [form] [form是layui.form对象]
* @param {string} [province] [省容器的name名字 ]
* eg:<select name="province">
* @param {string} [city] [省容器的name名字]
* @param {string} [district] [县容器的name名字]
* @return {undefined} [无返回值]
*/
showCity: function(province, city, district) {
//AreaData是地址传过来的json对象
var AreaData = $.rawCitiesData;
// console.log(999, AreaData) // 接口请求数据
if("undefined" == typeof AreaData) {
layer.open({
title: '系统提示'
,content: '调用showCity之前请引入地址数据'
});
return;
}
var htmlProvince = '',
htmlCity = '',
htmlDistrict = '',
cityData = '',
districtData = '',
$province = $('select[name='+province+']'),
$city = $('select[name='+city+']'),
$district = $('select[name='+district+']'),
provVal = $province.attr('data-area') || '', // 省默认值
cityVal = $city.attr('data-area') || '', // 市默认值
distVal = $district.attr('data-area') || ''; // 县默认值
$province.find('option').not(':first').remove();
$city.find('option').not(':first').remove();
$district.find('option').not(':first').remove();
form.render('select');
// 加载省数据
loadProvince();
function loadProvince() {
AreaData.forEach(function(v, i) {
htmlProvince += '<option value='+v.code+'>'+v.name+'</option>';
});
$province.append(htmlProvince);
// 默认选中省
defaultSelect($province, provVal, function(code) {
cityData = findPlace(AreaData, code);
loadCity(cityData, true);
});
form.render('select');
}
// 加载城市数据
function loadCity(city, hasDefault) {
$city.find('option').not(':first').remove();
htmlCity = '';
var city = city || [];
city.forEach(function(v, i) {
htmlCity += '<option value='+v.code+'>'+v.name+'</option>';
});
$city.append(htmlCity);
// 默认加载才执行,手动选中不执行
if (hasDefault) {
// 默认选中城市
defaultSelect($city, cityVal, function(code) {
// console.log(code)
// 加载默认城市数据
districtData = findPlace(cityData, code);
loadDistrict(districtData, true);
});
}
form.render('select');
}
// 加载县数据
function loadDistrict(districtData, hasDefault) {
$district.find('option').not(':first').remove();
htmlDistrict = '';
var districtData = districtData || [];
districtData.forEach(function(v, i) {
htmlDistrict += '<option value='+v.code+'>'+v.name+'</option>';
});
$district.append(htmlDistrict);
if (hasDefault) {
// 默认选中县
defaultSelect($district, distVal);
}
form.render('select');
}
// 查找地点数据
function findPlace(data, code) {
var resData = [];
data.forEach(function(v, i) {
if (v.code === code) {
resData = v.sub
}
});
return resData;
}
/*
* 查找默认选中项
* @param {object} jq元素
* @param {string} 默认值
* @param {function} 回调函数
*/
function defaultSelect($el, SelectVal, callback) {
$('option', $el).each(function(i, el) {
var $this = $(this);
var optVal = $this.html();
var code;
if (optVal == SelectVal) {
$this.attr("selected", true);
code = $this.val();
// 加载默认城市数据
callback && callback(code);
}
});
}
// 省选择
form.on('select('+province+')', function(data) {
var code = data.value;
// console.log(data.elem); //得到select原始DOM对象
// console.log(code); //得到被选中的值
if (code != '') {
cityData = findPlace(AreaData, code);
loadCity(cityData);
loadDistrict();
} else {
loadCity();
loadDistrict();
}
// console.log(data.othis); //得到美化后的DOM对象
});
// 市选择
form.on('select('+city+')', function(data){
var code = data.value;
if (code != '') {
districtData = findPlace(cityData, code);
loadDistrict(districtData);
// console.log(data.elem); //得到select原始DOM对象
} else {
loadDistrict();
}
// console.log(data.othis); //得到美化后的DOM对象
});
form.on('select('+district+')', function(data){
// console.log(data.elem); //得到select原始DOM对象
// console.log(data.value); //得到被选中的值
// console.log(data.othis); //得到美化后的DOM对象
});
},
/**
* 获取省市县数据
*
* @param { object } [address] [address eg:广东省广州市天河区]
* @return {object} [address][根据code码返回地址名称]
*/
getCity: function(address) {
//AreaData是地址传过来的json对象
var AreaData = $.rawCitiesData;
if("undefined" == typeof AreaData) {
layer.open({
title: '系统提示'
,content: '调用getCity之前请引入地址数据'
});
return;
}
var province = address.province,
city = address.city,
district = address.district,
provinceName = '',
cityName = '',
districtName = '';
function findIndex(arr, target) {
return arr.findIndex(function(v, i) {
return target == v.code;
})
}
if (province) {
var findProvinceIndex = findIndex(AreaData, province);
provinceName = AreaData[findProvinceIndex].name;
}
if (province && city) {
var findCityIndex = findIndex(AreaData[findProvinceIndex].sub, city);
cityName = AreaData[findProvinceIndex].sub[findCityIndex].name;
}
if (province && city && district) {
var findDistrictIndex = findIndex(AreaData[findProvinceIndex].sub[findCityIndex].sub, district);
districtName = AreaData[findProvinceIndex].sub[findCityIndex].sub[findDistrictIndex].name;
}
return {
provinceName: provinceName,
cityName: cityName,
districtName: districtName
}
}
}
// exports module
exports('common', returnObj);
});