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