jquery.linkage_select.js
6.76 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
/**
* 多级联动插件
* @author ln
*
*
# 示例html
<div class="region_select"></div>
# 示例css
.region_select{height:38px;}
.region_select select{padding:8px 0;line-height:30px;min-width:140px;margin-right:20px;}
.region_select .invalid{border:1px solid #f60;}
# 示例javascript
<script type="text/javascript">
$(".region_select").LinkageSelect({
// 数据源
dataUrl:"http://manweb.nong12.com/city/getCountryJsonpList.do",
// 字段映射
map: {
id:"regionId",
name:"regionName",
pid:"parentId",
},
// 分类起始
pid:"-10",
// 默认值
selected:["111","1110101"],
// form表单名称
inputName:"region",
// 提示标签
default_labels: {
0: "请选择",
1: "请选择",
2: "请选择",
3: "请选择",
4: "请选择"
}
});
</script>
*/
;(function($) {
/**
* 获取数据资源,返回数据统一使用 {key:value}
* @type {Object}
*/
var dataSource = {
common: function(pid, records_ids, select_index, cb) {
var default_labels = this.opt.default_labels;
var idField = this.idField;
var nameField = this.nameField;
var pidField = this.pidField;
$.ajax({
url: this.opt.dataUrl + "?" + pidField + "=" + pid,
dataType: "jsonp",
success: function(data) {
var hash = [];
if (!data.length) {
return;
}
if (default_labels[select_index]) {
hash.push({
id: "",
name: default_labels[select_index]
});
}
$.each(data, function(index, one) {
hash.push({
id: one[idField],
name: one[nameField]
});
});
return cb(hash);
},
error: function(e) {}
});
}
};
function LinkageSelect($container, options) {
this.$container = $container;
// 可选项
this.opt = {
level: 9999,
// 数据url
dataUrl: "",
dataSource: "common",
// 拓展数据源 参考 dataSource 对象写法
dataSourceFn: {},
// 数据源字段映射, 例如 {id:"regionId"}
map: {
},
// 父级ID
pid: 0,
inputName: "region_select" + Math.round(Math.random() * 100),
// 默认选中
selected: [],
// 默认标签
default_labels: {
0: "请选择",
1: "请选择",
2: "请选择",
3: "请选择",
4: "请选择"
}
};
if (typeof dataSource[this.opt.dataSource] === "undefined") {
return;
}
$.extend(this.opt, options);
// 拓展数据源
$.extend(dataSource, this.opt.dataSourceFn);
// 字段映射
this.idField = this.opt.map['id'] || "id";
this.nameField = this.opt.map['name'] || "name";
this.pidField = this.opt.map['pid'] || "pid";
this.Init();
}
LinkageSelect.prototype = {
isInit: false,
Init: function() {
var that = this;
this.$container.css('opacity', 0);
setTimeout(function() {
that.$container.animate({
opacity: 1
});
}, 500);
this.renderSelect(this.opt.pid, 0);
this.bindEvent();
},
// 绑定事件
// 如果一个select change 需要移除后面的select 然后重建后后面的select
bindEvent: function() {
var that = this;
this.$container.on('change', 'select', function(event) {
event.preventDefault();
var has_next = $(this).next();
var index = $(this).index();
var value = $(this).val();
$(this).removeClass('invalid');
if (!value || !has_next || (index + 1) >= that.opt.level) {
that.$container.children(':gt(' + index + ')').remove();
return;
}
that.renderSelect(value, index + 1);
});
},
renderSelect: function(pid, index) {
var that = this;
dataSource[this.opt.dataSource].call(this, pid, this.records_ids, index, $.proxy(function(data) {
if (typeof index !== "undefined") {
var current_index = !index ? 0 : index - 1;
this.$container.children(':gt(' + current_index + ')').remove();
}
if (data.length < 1) {
return;
}
var $select = $('<select name="' + that.opt.inputName + '[' + index + ']" pid="' + pid + '" index="' + index + '">');
var options = [];
$.each(data, function(key, value) {
if (value.id == that.opt.selected[index]) {
options.push('<option selected value="' + value.id + '">' + value.name + '</option>');
} else {
options.push('<option value="' + value.id + '">' + value.name + '</option>');
}
});
$select.html(options.join(""));
this.$container.append($select);
$select.trigger('change');
this.$container.trigger('renderSelectEnd');
}, this));
}
};
if (window.jQuery || window.Zepto) {
(function($) {
'use strict';
$.fn.LinkageSelect = function(params) {
var firstInstance;
this.each(function(i) {
var that = $(this);
if (!that.data('LinkageSelect')) {
var s = new LinkageSelect(that, params);
if (!i) firstInstance = s;
that.data('LinkageSelect', s);
}
});
return firstInstance;
};
})(window.jQuery || window.Zepto);
}
if (typeof define === 'function' && define.amd) {
define([], function() {
'use strict';
return LinkageSelect;
});
}
})(jQuery);