-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.dynamic-select.js
152 lines (100 loc) · 4.09 KB
/
jquery.dynamic-select.js
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
/**
* Dynamic Select
* Populate and repopulate a select on the fly.
*
* Copyright (c) 2013 PJ Dietz
* Version: 1.1.1
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
* http://pjdietz.com/jquery-plugins/dynamic-select/
*/
/*global jQuery */
(function ($) {
"use strict";
var DynamicSelect;
DynamicSelect = function (element, options) {
// Merge the options.
this.options = $.extend({}, $.fn.dynamicSelect.defaults, options);
// Store a reference to the element.
this.element = $(element);
}; // DynamicSelect
DynamicSelect.prototype = {
update: function (options) {
var option, subObtion, optionElem, optgroupElem, i, u, j, v;
// Clear out the old items.
this.element.empty();
// Filter the options.
options = this.options.filter(options);
for (i = 0, u = options.length; i < u; i += 1) {
// Reference the current option.
option = options[i];
if (!$.isArray(option.value)) {
// The value is not an array. Add an option.
optionElem = $("<option></option>")
.attr("value", option.value)
.text(option.label);
// Append the option to the select.
this.element.append(optionElem);
} else {
// The value is an array. Add an optgroup.
optgroupElem = $('<optgroup></optgroup>')
.attr('label', option.label);
// Add the sub-options to the optgroup.
for (j = 0, v = option.value.length; j < v; j += 1) {
// Reference the current sub-option.
subObtion = option.value[j];
// Make an option element.
optionElem = $("<option></option>")
.attr("value", subObtion.value)
.text(subObtion.label);
// Append the option to the optgroup.
optgroupElem.append(optionElem);
}
// Append the optgroup to the select.
this.element.append(optgroupElem);
} // if
} // for
} // update()
}; // DynamicSelect.prototype
// -------------------------------------------------------------------------
// Extend jQuery
if (typeof $.fn.freeow === "undefined") {
$.fn.extend({
dynamicSelect: function (method, options) {
// If only one parameter was passed, assume the caller means
// to use the contructor with options.
if (typeof options === "undefined") {
options = method;
}
return this.each(function () {
var ds;
// Try to read the TabSet from the element.
ds = $(this).data("DynamicSelect");
// If no DynamicSelect exists, create one with the options.
if (typeof ds === "undefined") {
ds = new DynamicSelect($(this), options);
$(this).data("DynamicSelect", ds);
}
switch (method) {
case "set":
ds.set(options);
break;
case "setFilter":
ds.setFilter(options);
break;
case "update":
ds.update(options);
break;
} // switch
}); // this.each()
} // dynamicSelect()
}); // $.fn.extend()
// Configuration Defaults.
$.fn.dynamicSelect.defaults = {
filter: function (options) {
return options;
}
}; // $.fn.dynamicSelect.defaults
} // if undefined
}(jQuery));