MultiLevel.js
10.6 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
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
/**
* founder : zzf
* Creation time : 2015/3/18
*/
define(function( require, exports, module ) {
var regions = [],sorts = [];//对象管理池
var MultiLevel = function(){
var config = {
data : {},
view : "",
parameter : {
url : "",
setUrl : "",
id : 0,
field : {
pid : "parentId",
did : "regionId",
name : "regionName"
},
param : "parentId",
setNmae : "city"
},
heads : [],
sub : 0,
constraintTier : 0,
page : 0,
isEvents : true
};
var methods = {
foundView : function(){
var view = '<div class="title"><div title=""></div>'
+'<span class="s-live"></span>'
+'<p class="region-data"></p></div>'
+'<div class="region-view">'
+'<div class="head"><ul></ul></div>'
+'<div class="cont clearfix"><ul></ul></div></div>';
config.view.html(view);
this.parseParam();
},
parseParam : function(){
if(config.view.attr("setUrl")){
config.parameter.setUrl = config.view.attr("setUrl");
methods.setValue();
return false;
}
try{
var param = config.view.attr("data");
if(!param){methods.http(); return false;}
param = param.split(",");
config.parameter.setNmae = param[0];
config.parameter.field.pid = param[1];
config.parameter.field.did = param[2];
config.parameter.field.name = param[3];
config.parameter.param = param[4];
config.constraintTier = parseInt(param[5]);
}catch (error){
methods.error("朋友参数传递错误了,请仔细检查下", error);
};
methods.http();
},
setUrl : function(){
if(config.view.attr("url")){
config.parameter.url = config.view.attr("url");
}else{
methods.error("朋友参数url没有设置,请仔细检查下");
};
config.parameter.url = config.parameter.url+'?'+config.parameter.param+'='+config.parameter.id;
},
http : function(){
if(methods.getStorage(config.parameter.id)){
methods.setView(methods.getStorage(config.parameter.id));
return true;
};
methods.setUrl();
$.ajax({
type : 'GET',
url : config.parameter.url,
dataType : 'jsonp',
success : function(data){
methods.setStorage(data);
},
error : function(error){
methods.error("请求数据异常~~~", error);
}
});
},
setStorage : function(data){
if(data && data.length > 0){
config.data[config.parameter.id] = data;
methods.setView(data);
}else{
methods.setData();
};
},
getStorage : function(id){
if(config.data[id]){
return config.data[id];
}else{
return false;
}
},
setView : function(data){
methods.setHead(data);
methods.setCont(data);
if(config.isEvents){
methods.events();
config.isEvents = false;
};
},
setHead : function(data){
config.view.find(".head ul").html('');
var view = "";
for(i in config.heads){
view += '<li class="" zindex="'+config.page+'" pid="'
+config.heads[i].id+'">'
+config.heads[i].name+'</li>';
}
view += '<li class="act" zindex="'+config.page+'" pid="">请选择</li>';
config.view.find(".head ul").html(view);
},
setCont : function(data){
config.view.find(".cont ul").html('');
var view = "";
for(i in data){
view += '<li class="" zindex="'+config.page+'" pid="'
+data[i][config.parameter.field.pid]+'" did="'
+data[i][config.parameter.field.did]+'">'
+data[i][config.parameter.field.name]+'</li>';
}
config.view.find(".cont ul").html(view);
},
setData : function(data){
var list = [];
var id = 0;
var str = "";
if(data){
list = data;
}else{
list = config.heads;
};
for(i in list){
str += " "+list[i].name;
if(parseInt(i) == (list.length - 1)){
id = list[i].id;
};
}
var isInput = config.view.prevAll(".region-hide");
if(isInput.attr("type") && isInput.attr("type") == "hidden"){
isInput.val(str+"/"+id);
}else{
var view = '<input type="hidden" class="region-hide" name="'+config.parameter.setNmae+'" value="'+(str+"/"+id)+'" />';
config.view.before($(view));
isInput.val(str+"/"+id);
}
config.view.find(".region-data").html(config.heads);
config.view.find(".title div").html(str);
config.view.find(".region-view").hide();
},
headDel : function(id){
var page = 0;
for(i in config.heads){
if(config.heads[i].id == id){
page = parseInt(i);
}
}
config.heads.splice(page, (config.heads.length - page));
methods.http();
},
headIsNmae : function(view){
var text = view.html();
var pid = view.attr("pid");
for(i in config.heads){
if(config.heads[i].id == pid){
config.heads[i] = {
id : pid,
name : text,
did : view.attr("did")
}
methods.setData();
return false;
}
}
if(text != "其他区县" && text != "其他城市"){
return true;
}
return false;
},
events : function(){
config.view.find(".head li").live("click", function(){
config.parameter.id = $(this).attr("pid");
methods.headDel(config.parameter.id);
});
config.view.find(".cont li").live("click", function(){
config.parameter.id = $(this).attr("did");
if(methods.headIsNmae($(this))){
config.heads.push({id : $(this).attr("pid"),
name : $(this).html(),
did : $(this).attr("did")
});
methods.http();
}
});
config.view.hover(function(){
$(this).find(".region-view").show();
}, function(){
$(this).find(".region-view").hide();
});
},
error : function(txt, error){
alert(txt+", 详情请查看控制台log");
if(error){console.log(error)};
},
setValue : function(){
$.ajax({
type : 'GET',
url : config.parameter.setUrl,
dataType : 'jsonp',
success : function(data){
data = data.reverse();
var list = [];
for(i in data){
list.push({
id : data[i][config.parameter.field.pid],
name : data[i][config.parameter.field.name],
did : data[i][config.parameter.field.did]
});
};
methods.setData(list);
methods.http();
},
error : function(error){
methods.error("请求数据异常~~~", error);
}
});
}
};
this.start = function(view, sub){
config.sub = sub;
config.view = $(view);
config.view.attr("sub", sub);
methods.foundView();
};
this.getData = function(){
methods.getData();
};
this.update = function(){
if(config.view.attr("setUrl")){
config.parameter.setUrl = config.view.attr("setUrl");
methods.setValue();
}
};
this.reset = function(){
config.heads = [];
config.parameter.id = 0;
methods.http();
};
return this;
};
exports.region = function(view){
var ml = new MultiLevel();
regions.push(ml);
ml.start(view, (regions.length - 1));
return ml;
};
exports.allregion = function(){
$(".m-region").each(function(){
if(!$(this).attr("sub")){
var ml = new MultiLevel();
regions.push(ml);
ml.start(this, (regions.length - 1));
};
});
};
exports.delete = function(page){
if(!page){return false};
$(".m-region").each(function(i){
if(i == page){
regions.splice(i, 1);
return true;
};
});
return false;
};
});