-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
168 lines (163 loc) · 4.96 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>vue2.0</title>
<link rel="stylesheet" href="css/zui.min.css">
</head>
<script src="../lib/vue2.0.js"></script>
<script src="lib/jquery/jquery.js"></script>
<body>
<div class="container-fluid">
<div class="main">
<!-- 城市选择 -->
<div id="citylist">
<input type="hidden" id="recievecity_id" value="1" name="cityselect">
</div>
</div>
</div>
</body>
<script>
// 模拟数据请求。
var result;
$.ajax( {
url : 'data/data.json',
type : 'GET',
async : false,
dataType : 'json',
error : function() {
alert('操作出现错误!');
},
success : function(data) {
if (data) {
result= data.data;
} else {
}
}
});
// 获取页面层的城市id
var recievecity_id = parseInt($("#recievecity_id").val());
// 递归查找
function getCtiy(data,cityId)
{
for (var i in data) {
if (data[i].id == cityId) {
// 找到对应id返回此对象。
recieveObj = data[i];
break;
} else {
getCtiy(data[i].child_citys, cityId);
}
}
}
// 此段代码跟上段代码逻辑一致,可优化,查到区县,根据id再次递归查找省份。
function getProvince(data,cityId){
for (var i in data) {
if (data[i].id == cityId) {
// 找到省份返回此对象。
recieveObjprovince = data[i];
break;
} else {
getProvince(data[i].child_citys, cityId);
}
}
}
getCtiy(result.citys,recievecity_id);
// 如对象的为三级结构。
if (recieveObj.level===3) {
recieveCity = recieveObj.parent_id;
getProvince(result.citys,recieveCity);
recieveProvince = recieveObjprovince.parent_id;
recieveCountry = recieveObj.id;
}else if (recieveObj.level===2) {
// 对象为二级结构
recieveCity = recieveObj.id;
recieveProvince = recieveObj.parent_id;
recieveCountry = 0;
}else{
// 对象为一级结构
recieveProvince = recieveObj.id;
recieveCity = 0;
recieveCountry = 0;
}
// template
var citytemplate = '<div class="form-group">'+
'<label class="col-md-2 control-label">城市:</label>'+
'<div class="col-md-2">'+
'<select v-model ="selectedProvinceId" class="form-control" id="rowprovince">'+
'<option v-for="province in provinces" :value="province.id">'+
'{{province.name}}'+
'</option>'+
'</select>'+
'</div>'+
'<div class="col-md-2">'+
'<select class="form-control" v-model="selectedCityId" id="rowcity">'+
'<option value="-1">请选择市</option>'+
'<option v-for="city in cities" :value="city.id">'+
'{{city.name}}'+
'</option>'+
'</select>'+
'</div>'+
'<div class="col-md-2">'+
'<select class="form-control" v-model="selectedCountryId" id="rowcountry">'+
'<option value="-1">请选择区县</option>'+
'<option v-for="country in countres" :value="country.id">'+
'{{country.name}}'+
'</option>'+
'</select>'+
'</div>'+
'</div>'
// vue实例
var vm = new Vue({
el:'#citylist',
data(){
return {
selectedProvinceId: recieveProvince,
selectedCityId: -1,
selectedCountryId: -1,
provinces: result.citys, // 省
}
},
created:function(){
$("#citylist").append(citytemplate);
},
watch:{
selectedProvinceId:function(newselect,oldselect){
if (newselect!=oldselect) {
vm.selectedCityId = -1
vm.selectedCountryId = -1
}
},
selectedCityId:function(newselect,oldselect){
if (newselect!=oldselect) {
vm.selectedCountryId = -1
}
}
},
computed:{
cities: function() {
var tempcities =[];
//获取市数据
var selectedProvince = this.selectedProvinceId;
this.provinces.forEach(function(d){
if (d.id == selectedProvince ) {
tempcities = d.child_citys;
}
});
return tempcities
},
countres: function() {
var tempcounres =[];
// 获取区县数据
var selectedCity = this.selectedCityId;
this.cities.forEach(function(d){
if (d.id == selectedCity ) {
tempcounres = d.child_citys;
}
});
return tempcounres
}
}
})
</script>
</html>