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