-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfmCombo.js
More file actions
129 lines (104 loc) · 3.29 KB
/
fmCombo.js
File metadata and controls
129 lines (104 loc) · 3.29 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
/*
* fmcombo - jQuery plugin 0.1pre
*
* Copyright (c) 2011 Fabio Michelucci <fmichelucci@gmail.com>
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
(function( $ ){
var settings = {
source: null,
depElements: [],
nextElement: null,
firstOption: "-- seleziona --",
firstValue: "-1",
afterSelect: function () { },
onEmptySource: function () { },
childs:[]
};
var $fmcombo = {
source: null,
formatResult: function(data){
var opt='<option value="'+settings.firstValue+'">'+settings.firstOption+'</option>'
$.each(data,function(key,value){
opt += '<option value="'+data[key].Value+'">'+data[key].Text+'</option>';
});
return opt;
},
initSource: function(obj,elements,request){
if($.isArray(request)){
$fmcombo.source = $fmcombo.formatResult(request);
} else if (typeof request === "string"){
var url = request+$fmcombo.serialize(obj,elements)
self.xhr = $.ajax({
url: url,
dataType: "json",
async: false,
success: function (data) {
if (data.length > 0) {
$fmcombo.source = $fmcombo.formatResult(data);
}
else
{
settings.onEmptySource.call(this);
}
},
error: function(){alert('Si sono verificati errori!')}
});
} else if ($.isFunction(settings.source))settings.source.call($)
},
init : function(options) {
if ( options ) {$.extend(settings, options );}
$(this).change(function(){
$sel = $(this);
//debug($sel.attr("name"));
$fmcombo.initSource($sel, options.depElements, options.source);
$(options.nextElement).html($fmcombo.source).focus();
$fmcombo.sanitazeChilds(options.childs);
//$.isFunction(options.afterSelect)? options.afterSelect.call($): null
settings.afterSelect.call(this);
});
},
serialize: function(obj,elements){
var qs = '?'+obj.attr("name")+'='+encodeURIComponent(obj.val());
if($.isArray(elements)){
$.each(elements, function(key,value){
$e = $(value);
qs += '&'+$e.attr("name")+'='+encodeURIComponent($e.val());
});
} else if (typeof elements === "string"){
debug('Dep is string')
qs += '&'+$(elements).attr("name")+'='+encodeURIComponent($(elements).val())
}
return qs;
},
sanitazeChilds: function(elements){
if($.isArray(elements)){
$.each(elements, function(key,value){
$(value).empty()
});
} else if (typeof elements === "string"){
$(elements).empty()
}
}
};
//Plugin namespace
$.fn.fmcombo = function(method) {
// Method calling logic
if ( $fmcombo[method] ) {
return $fmcombo[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return $fmcombo.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.fmcombo' );
}
};
//private function for debugging
function debug(message) {
if (window.console && window.console.log)
window.console.log('fmCombo: ' + message);
}
})( jQuery );