forked from rajkaimal/CascadingDropDown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.cascadingDropDown.js
135 lines (125 loc) · 4.74 KB
/
jquery.cascadingDropDown.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
/*
* CascadingDropDown : a jQuery plugin, version: 0.2 (2010-05-21)
* @requires jQuery v1.4 or later
*
* CascadingDropDown is a jQuery plugin that can be attached to a select list to get automatic population.
* Each time the source element changes value, an ajax request is made to retrieve a list of values for
* the select list. The respose from the ajax request should be in json with the following format
*
* [{
* "Text": "John",
* "Value": "10326"
* },
* {
* "Text": "Jane",
* "Value": "10801"
* }]
*
*
* Licensed under the MIT:
* http://www.opensource.org/licenses/mit-license.php
*
* Raj Kaimal http://weblogs.asp.net/rajbk/
*
* v0.2 Added support for custom postData using functions
*
*
*/
(function ($) {
$.fn.CascadingDropDown = function (source, actionPath, settings) {
if (typeof source === 'undefined') {
throw "A source element is required";
}
if (typeof actionPath == 'undefined') {
throw "An action path is requried";
}
var optionTag = '<option></option>';
var config = $.extend({}, $.fn.CascadingDropDown.defaults, settings);
return this.each(function () {
var $this = $(this);
(function () {
var methods = {
clearItems: function () {
$this.empty();
if (!$this.attr("disabled")) {
$this.attr("disabled", "disabled");
}
},
reset: function () {
methods.clearItems();
$this.append($(optionTag)
.attr("value", "")
.text(config.promptText));
$this.trigger('change');
},
initialize: function () {
if ($this.children().size() == 0) {
methods.reset();
}
},
showLoading: function () {
methods.clearItems();
$this.append($(optionTag)
.attr("value", "")
.text(config.loadingText));
},
loaded: function () {
$this.removeAttr("disabled");
$this.trigger('change');
},
showError: function () {
methods.clearItems();
$this.append($(optionTag)
.attr("value", "")
.text(config.errorText));
},
post: function () {
methods.showLoading();
$.isFunction(config.onLoading) && config.onLoading.call($this);
$.ajax({
url: actionPath,
type: 'POST',
dataType: 'json',
data: ((typeof config.postData == "function") ? config.postData() : config.postData) || $(source).serialize(),
success: function (data) {
methods.reset();
$.each(data, function () {
$this.append($(optionTag)
.attr("value", this.Value)
.text(this.Text));
});
if (config.defaultValue != null) {
$this.val(config.defaultValue);
}
methods.loaded();
$.isFunction(config.onLoaded) && config.onLoaded.call($this);
},
error: function () {
methods.showError();
}
});
}
};
$(source).change(function () {
var parentSelect = $(source);
if (parentSelect.val() != '') {
methods.post();
}
else {
methods.reset();
}
});
methods.initialize();
})();
});
}
$.fn.CascadingDropDown.defaults = {
promptText: '-- Select --',
loadingText: 'Loading ..',
errorText: 'Error loading data.',
postData: null,
onLoading: null,
onLoaded: null,
defaultValue: null
}
})(jQuery);