22
|
1 |
/**
|
|
2 |
* Chained Selects for jQuery
|
|
3 |
* Copyright (C) 2008 Ziadin Givan www.CodeAssembly.com
|
|
4 |
*
|
|
5 |
* This program is free software: you can redistribute it and/or modify
|
|
6 |
* it under the terms of the GNU General Public License as published by
|
|
7 |
* the Free Software Foundation, either version 3 of the License, or
|
|
8 |
* (at your option) any later version.
|
|
9 |
*
|
|
10 |
* This program is distributed in the hope that it will be useful,
|
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 |
* GNU General Public License for more details.
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License
|
|
16 |
* along with this program. If not, see http://www.gnu.org/licenses/
|
|
17 |
*
|
|
18 |
*
|
|
19 |
* settings = { usePost : true, before:function() {}, after: function() {}, default: null, parameters : { parameter1 : 'value1', parameter2 : 'value2'} }
|
|
20 |
* if usePost is true, then the form will use POST to pass the parameters to the target, otherwise will use GET
|
|
21 |
* "before" function is called before the ajax request and "after" function is called after the ajax request.
|
|
22 |
* If defaultValue is not null then the specified option will be selected.
|
|
23 |
* You can specify additional parameters to be sent to the the server in settings.parameters.
|
|
24 |
*
|
|
25 |
*/
|
|
26 |
jQuery.fn.chainSelect = function( target, url, settings )
|
|
27 |
{
|
|
28 |
return this.each( function()
|
|
29 |
{
|
|
30 |
$(this).change( function( )
|
|
31 |
{
|
|
32 |
settings = jQuery.extend(
|
|
33 |
{
|
|
34 |
after : null,
|
|
35 |
before : null,
|
|
36 |
usePost : false,
|
|
37 |
defaultValue : null,
|
|
38 |
parameters : {'_id' : $(this).attr('id'), '_name' : $(this).attr('name')}
|
|
39 |
} , settings);
|
|
40 |
|
|
41 |
settings.parameters._value = $(this).val();
|
|
42 |
|
|
43 |
if (settings.before != null)
|
|
44 |
{
|
|
45 |
settings.before( target );
|
|
46 |
}
|
|
47 |
|
|
48 |
ajaxCallback = function(data, textStatus)
|
|
49 |
{
|
|
50 |
$(target).html("");//clear old options
|
|
51 |
data = eval(data);//get json array
|
|
52 |
for (i = 0; i < data.length; i++)//iterate over all options
|
|
53 |
{
|
|
54 |
for ( key in data[i] )//get key => value
|
|
55 |
{
|
|
56 |
$(target).get(0).add(new Option(data[i][key],[key]), document.all ? i : null);
|
|
57 |
}
|
|
58 |
}
|
|
59 |
|
|
60 |
if (settings.defaultValue != null)
|
|
61 |
{
|
|
62 |
$(target).val(settings.defaultValue);//select default value
|
|
63 |
} else
|
|
64 |
{
|
|
65 |
$("option:first", target).attr( "selected", "selected" );//select first option
|
|
66 |
}
|
|
67 |
|
|
68 |
if (settings.after != null)
|
|
69 |
{
|
|
70 |
settings.after(target);
|
|
71 |
}
|
|
72 |
|
|
73 |
$(target).change();//call next chain
|
|
74 |
};
|
|
75 |
|
|
76 |
if (settings.usePost == true)
|
|
77 |
{
|
|
78 |
$.post( url, settings.parameters, ajaxCallback );
|
|
79 |
} else
|
|
80 |
{
|
|
81 |
$.get( url, settings.parameters, ajaxCallback );
|
|
82 |
}
|
|
83 |
});
|
|
84 |
});
|
|
85 |
}; |