-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.jd-remote-select.js
168 lines (140 loc) · 4.09 KB
/
jquery.jd-remote-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
153
154
155
156
157
158
159
160
161
162
163
164
/*
Remote Select form plugin
Copyright © 2012 Jordi Dosne
http://www.dosne.net
Requires jQuery 1.4 or newer
Requires jQuery Metadata
Allow to a select form to get json data.
Enjoy!
*/
(function($) {
$.jdRemoteSelect = {
defaults: {
keepFirstOption : false,
keepLastOption : false,
subSelect : null,
labelProperty : "label",
valueProperty : "id",
autoLoad : true,
defaultValue : '',
data: {}
},
elements: []
};
var methods =
{
init : function(options)
{
return this.each(function()
{
var $this = $(this);
if( $this.metadata === undefined ) {
throw new Error('jQuery Metadata must be imported'); }
// If the plugin hasn't been initialized yet
if ( !$.data(this, 'jdRemoteSelectParams') )
{
var o = $.parseJSON($this.attr('data-remote-select'));
var params = $.extend({}, $.jdRemoteSelect.defaults, o, options);
params.data.method = "get";
if( params.subSelectTarget !== undefined && params.subSelectTarget !== "" ) {
params.subSelect = $(params.subSelectTarget); }
else {
params.subSelect = null; }
$.data(this, 'jdRemoteSelectParams', params);
if( params.data.url === undefined || params.data.url === "" ) {
return; }
if( params.autoLoad === true )
{
//$this.jdRemoteSelect('loadData');
methods.loadData.apply($this, []);
}
params = null;
}
});
},
loadData : function( )
{
var $this = this;
var params = $.data($this[0], 'jdRemoteSelectParams');
$this.attr('disabled', 'disabled');
var result = $.parseJSON($.ajax(
{
url : params.data.url,
dataType: 'json',
data : params.data,
async: true,
success: function(result)
{
var len = result.length;
if( result.length === 0 )
{
$this.hide();
return;
}
if( params.keepFirstOption === true ) {
$this.find('option:gt(0)').remove(); }
else if( params.keepLastOption === true ) {
$this.find('option:lt('+($this.find('option').length-1)+')').remove(); }
else {
$this.find('option').remove(); }
// Populate select
for( var i = 0; i < len; i++ )
{
$this.append("<option value="+result[i][params.valueProperty]+">"+result[i][params.labelProperty]+"</option>");
}
// Set default Value
if( params.defaultValue !== '' )
{
$this.find('option:selected').removeAttr('selected');
$this.find('option[value='+params.defaultValue+']').attr('selected', 'selected');
}
// Show the select
$this.removeAttr('disabled');
$this.show();
// If sub select is set, initialize it and link changeEvent to it
if( params.subSelect !== null && params.subSelect.length > 0 )
{
var sub = params.subSelect;
sub.jdRemoteSelect({autoLoad:false});
sub.hide();
$this.bind('change', function(e)
{
e.preventDefault();
var $this = $(e.target);
var dataSubSelect = $.data(sub[0], 'jdRemoteSelectParams').data[$this.attr('name')] = $this.val();
methods.loadData.apply(sub, []);
});
if( params.defaultValue != undefined && params.defaultValue != '' )
{
if( $.data(sub[0], 'jdRemoteSelectParams')) {
$.data(sub[0], 'jdRemoteSelectParams').data[$this.attr('name')] = $this.val(); }
methods.loadData.apply(sub, []);
}
}
}
}));
},
set : function( key, value )
{
var $this = $(this);
var params = $this.jdRemoteSelectParams;
params[key] = value;
},
get : function( key )
{
var $this = $(this);
var params = $this.jdRemoteSelectParams;
return params[key];
}
};
$.fn.jdRemoteSelect = function(methodOrOptions) {
if ( methods[methodOrOptions] ) {
return methods[ methodOrOptions ].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof methodOrOptions === 'object' || ! methodOrOptions ) {
// Default to "init"
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + methodOrOptions + ' does not exist on jQuery.tooltip' );
}
};
})(jQuery);