-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjquery.autoPopulateBox.js
184 lines (147 loc) · 3.91 KB
/
jquery.autoPopulateBox.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
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
/*
* Class: autoPopulateBox
* Use: Auto Populate Select box using jquery
* Author: John Laniba (http://pixelcone.com)
* Version: 1.0
*/
(function($){
$.autoPopulateBox = {version: '1.0'};
$.fn.autoPopulateBox = function(config) {
config = $.extend({}, {
loader: false, //image loader url
triggerOnLoad: false,
disableBoxOnChange: true,
emptyLabel: false,
allOptionAsEmpty: false,
url: '',
allSeparator: false,
queryFirst: '?',
queryOperator: '=',
appendSlash: false //add slash "/" at the end of the url
}, config);
//configure separator
if (config.allSeparator) {
config.queryFirst = config.allSeparator,
config.queryOperator = config.allSeparator;
}
//ajax loader
if (config.loader) {
$(this).before($('<img />').attr({
'src': config.loader,
'alt': 'loading',
'id': 'apb-loader'
}).hide());
}
//parent select box config
var $target = {},
url = '', selected = '', value = '',
$self = null,
autoBox = {
onChange: function($self, $change) {
$target = $($change.target);
//Store Selected option
selected = $target.val();
if (config.emptyLabel) {
$target.empty().append($('<option>').text(config.emptyLabel).val(""));
} else {
$target.empty();
}
//prepend all option
if (config.allOptionAsEmpty) {
$target.prepend($('<option>').text(config.allOptionAsEmpty).val(""));
}
//if the value is empty dont to proceed to ajax call
if ( !$self.val() ) {
autoBox.complete($self, $change);
return;
}
//show loader
if (config.loader) {
$('#apb-loader').show().insertAfter($self);
}
if (config.disableBoxOnChange) {
$self.attr('disabled', 'disabled');
}
url = config.url;
if (typeof($change.url) != 'undefined'){
url = $change.url;
}
//append to URL if exist
if ($.isFunction($change.appendToUrl)){
url += $change.appendToUrl();
} else {
//remove the last slash on url
url = url.replace(/\/$/g, "");
url += config.queryFirst;
//get attribute name
if ($self.attr('apBoxName') != null) {
$name = $self.attr('apBoxName');
} else {
$name = $self.attr('name');
}
value = $self.val().toString();
url += $name + config.queryOperator + value.replace(/\,/g, "-");
}
if (config.appendSlash) {
url += '/';
}
//Call AJAX and return data as JSON object
$.ajax({
url: url,
type: 'get',
dataType: 'json'
})
.success(function(data) {
//Display data on your format
if ($.isFunction($change.formatData)) {
$change.formatData($target, data);
} else {
$.each(data, function(id,value){
if (value) {
$target.append( $("<option/>").text(value).val(id) );
}
});
}
if (selected) {
$target.val(selected);
}
})
.complete(function(jqXHR, textStatus) {
autoBox.complete($self, $change);
});
},
complete: function($self, $change) {
$('#apb-loader').hide();
$self.removeAttr('disabled');
if (typeof($change.change) != 'undefined'){
$($change.target).trigger('change');
}
//callback
if ($.isFunction($change.callback)) {
$change.callback($self);
}
},
changeConfig: function() {
/*if ($.isFunction(config.changeCallback)) {
config.change = config.changeCallback();
}*/
}
}
//Parent configuration
$(this).change(function(){
autoBox.onChange($(this), config[config.change]);
});
if (config.triggerOnLoad){
$(this).trigger('change');
}
//Children configuration
$.each(config, function(index, val){
if (typeof(config[index].change) != 'undefined') {
$(val.target).change(function(){
autoBox.onChange($(this), config[val.change]);
});
}
});
return this;
}
})(jQuery);