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