( ! ) Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'WPtouchPlugin::wptouch_parse_request' was given in C:\Domains\adamestrada.com\wwwroot\wp-includes\plugin.php on line 414
Call Stack
#TimeMemoryFunctionLocation
10.000962096{main}( )..\wp-404-handler.php:0
20.001467888include( 'C:\Domains\adamestrada.com\wwwroot\index.php' )..\wp-404-handler.php:7
30.001868184require( 'C:\Domains\adamestrada.com\wwwroot\wp-blog-header.php' )..\index.php:17
40.95242611944wp( )..\wp-blog-header.php:14
50.95242612224WP->main( )..\functions.php:1434
60.95252612328WP->parse_request( )..\classes.php:490
70.96932632616do_action_ref_array( )..\classes.php:293
80.96952633736call_user_func_array ( )..\plugin.php:414
jQuery Cascading Dropdown Menus | Adam Estrada

jQuery Cascading Dropdown Menus

2009 June 15
by Adam Estrada

One of the trickiest things to do gracefully on a website is create drop-down menus that are dependent on each other (also called cascading drop down menus). There are several ways to do this, of course, but I’ve seen that the most elegant/Web 2.0ish way to go about it is using AJAX calls to a web service or other exterior files. Ajax is a great way to grab data off your server because you DO NOT have to refresh the page once it’s loaded. I stumbled across this script on the jQuery examples page and expanded upon it to make it work the way I needed it to. Note the different types of cascading drop down menus on that site. The idea here is that a particular value in the first menu is selected and the script calls data sitting on the server and does an ad-hoc query to find related items from the JSON-formatted list.

?View Code JAVASCRIPT
1
[{"When":"Level_1","Value":"Level_2","Text":"Level_2"}]

Note how “Level_2″ is only activated when “Level_1″ is selected in the main script.

?View Code JAVASCRIPT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<script src="js/cascade/jquery.cascade.js" type="text/javascript"></script>
<script src="js/cascade/jquery.cascade.ext.js" type="text/javascript"></script>
<script type="text/javascript" src="js/cascade/jquery.templating.js" ></script>
 
<script type="text/javascript">
        jQuery(document).ready(function()
        {   
	     jQuery("#level_2").cascade("#level_1",{                   
                ajax: {url: 'js/level_2.js' }, 
                template: commonTemplate,
                match: commonMatch, 
			timeout: 250 //just to show loading indicator
            });
            jQuery("#level_3").cascade("#level_2",{                   
                ajax: {url: 'js/level_3.js' }, 
                template: commonTemplate,
                match: commonMatch, 
	        timeout: 250 //just to show loading indicator
            });
        });
</script>

In the above example, you’ll note that “Level_3″ is dependent on “Level_2 and “Level_2″ is dependent on “Level_1″ which is the cascading effect we were going for. It doesn’t get much easier than that! As long as the JSON formatted string reflects what you are querying for, i.e. “Level_2″ is dependent on “Level_1″ you’ll be good to go and this script will work like a charm. I’ve managed to get it to work on 6 different cascading levels without any problems at all. The author even added a cool little graphic that displays while the script is loading.

1
2
3
4
5
<style type="text/css">
.cascade-loading {
	background: transparent url("graphics/indicator.gif") no-repeat center;
}
</style>

There are many, many ways to do this but if you need a data-driven set of cascading drop down menus, I’ve found that this way is incredibly easy to implement and looks nice as well.

Bookmark and Share
No comments yet

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS

Spam protection by WP Captcha-Free