1,376 articles and 9,196 comments as of Tuesday, February 9th, 2010

Wednesday, December 3, 2008

JQuery for Everyone: Accordion Left Nav

Last night, I set out on a mission; a mission to create an accordion-style left navigation menu for my WSS test site.  Mission complete.  If you can copy/paste, you can see it in action.  Since I used Google’s API to load jQuery, you don’t even need to download the library file.

First, look at your left nav.  If you changed it from the out-of-the-box setup, make sure you have “headers” and “submenus” that make sense.  For example, Documents should appear on top of a bulleted list of document libraries.

Obviously, if you want the accordion-style menu for all pages, you should work it into the default.master.  For now, we can work with a test page by adding a Content Editor Web Part (CEWP) to the page.  Add the code below to the web part’s Content Editor (source).  Now your menu should look like this.

When you click on the menu header box with the down arrow image, it exposes the submenu below it and swaps the image with an ‘x’.  Likewise, clicking the header with the ‘x’ will hide the associated submenu.

Here’s the code:

<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
  // Load jQuery
  google.load("jquery", "1.2.6");
</script>
<script type="text/javascript">
$(function(){
//initialize menus
    var menuRows = $("[id$='QuickLaunchMenu'] > tbody > tr");
    var menuHd = menuRows.filter("[id!='']:has(+tr[id=''])");
    //set img path for when submenu is hidden
    var closedImg = "/_layouts/images/Menu1.gif";
    //set img path for when submenu is visible
    var openedImg = "/_layouts/images/ptclose.gif";
    var cssInit = {
        "background-image": "url('"+closedImg+"')",
        "background-repeat": "no-repeat",
        "background-position": "100% 50%"
    }
    var cssClosed = {"background-image": "url('"+closedImg+"')"}
    var cssOpen = {"background-image": "url('"+openedImg+"')"}
    //hide submenus
    menuRows.filter("[id='']").hide();
    //apply initial inline style to menu headers
    menuHd.find("td:last").css(cssInit);
    menuHd.click(function () {
        var styleElm = $(this).find("td:last")
        var nextTR = $(this).next("tr[id='']");
        if (nextTR.is(':visible')) {
            nextTR.hide();
            styleElm.css(cssClosed);
        } else {
            nextTR.show();
            styleElm.css(cssOpen);
        }
    });
});
</script>
View all entries in this series: PaulGrenier-JQuery for Everyone»
Entries in this series:
  1. JQuery for Everyone: Accordion Left Nav
  2. JQuery for Everyone: Print (Any) Web Part
  3. JQuery for Everyone: HTML Calculated Column
  4. JQuery for Everyone: Dressing-up Links Pt1
  5. JQuery for Everyone: Dressing-up Links Pt2
  6. JQuery for Everyone: Dressing-up Links Pt3
  7. JQuery for Everyone: Cleaning Windows Pt1
  8. JQuery for Everyone: Cleaning Windows Pt2
  9. JQuery for Everyone: Fixing the Gantt View
  10. JQuery for Everyone: Dynamically Sizing Excel Web Parts
  11. JQuery for Everyone: Manually Resizing Web Parts
  12. JQuery for Everyone: Total Calculated Columns
  13. JQuery for Everyone: Total of Time Differences
  14. JQuery for Everyone: Fixing Configured Web Part Height
  15. JQuery for Everyone: Expand/Collapse All Groups
  16. JQuery for Everyone: Preview Pane for Multiple Lists
  17. JQuery for Everyone: Preview Pane for Calendar View
  18. JQuery for Everyone: Degrading Dynamic Script Loader
  19. JQuery for Everyone: Force Checkout
  20. JQuery for Everyone: Replacing [Today]
  21. JQuery for Everyone: Whether They Want It Or Not
  22. JQuery for Everyone: Linking the Attachment Icon
  23. JQuery for Everyone: Aspect-Oriented Programming with jQuery
  24. JQuery for Everyone: AOP in Action - loadTip Gone Wild
  25. JQuery for Everyone: Wiki Outbound Links
  26. JQuery for Everyone: Collapse Text in List View
  27. JQuery for Everyone: AOP in Action - Clone List Header
  28. JQuery for Everyone: $.grep and calcHTML Revisited
  29. JQuery for Everyone: Evolution of the Preview
  30. JQuery for Everyone: Create a Client-Side Object Model
  31. JQuery for Everyone: Print (Any) Web Part(s) Plugin
  32. JQuery for Everyone: Minimal AOP and Elegant Modularity
  33. JQuery for Everyone: Cookies and Plugins
  34. JQuery for Everyone: Live Events vs. AOP
  35. JQuery for Everyone: Live Preview Pane
  36. JQuery for Everyone: Pre-populate Form Fields
  37. JQuery for Everyone: Get XML List Data with OWSSVR.DLL (RPC)
  38. Use Firebug in IE
  39. JQuery for Everyone: Extending OWS API for Calculated Columns
  40. JQuery for Everyone: Accordion Left-nav with Cookies Speed Test
  41. JQuery for Everyone: Email a List of People with OWS
  42. JQuery for Everyone: Faster than Document.Ready
  43. jQuery for Everyone: Collapse or Prepopulate Form Fields
  44. jQuery for Everyone: Hourly Summary Web Part
  45. jQuery for Everyone: "Read More..." On a Blog Site
  46. jQuery for Everyone: Slick Speed Test
  47. jQuery for Everyone: The SharePoint Game Changer
  48. JQuery For Everyone: Live LoadTip
 

Please Join the Discussion

81 Responses to “JQuery for Everyone: Accordion Left Nav”
  1. Very nice.

    I have copied the script into our free infuser tool to automatically apply this script to every page in a site collection.

    Blogged about it here http://www.muhimbi.com/blog/2009/07/massage-sharepoint-into-submission.html

  2. Frank says:

    Is there away to ‘hide’ the Quick Launch. I would like to do away with it if possible. Hope you can help.

  3. AutoSponge says:

    @Frank,

    If you want it dynamic, try this:
    http://www.endusersharepoint.com/STP/topic/dynamically-hiding-left-navigation

    If you want it to default to closed, just add leftnav.click() after the toggle method.

  4. AutoSponge says:

    @Jeroen Ritmeijer,

    Cool!

  5. Frank says:

    Thanks all, I got what I need. Have a great day :)

  6. Jason says:

    Thanks works 90% of what I needed, now just need to make it stay open to when select page below heading.
    Then get the mouseover to work correctly.

  7. AutoSponge says:

    @Jason,

    To “stay open,” I assume you mean between page loads: check out the second generation of this script: http://www.endusersharepoint.com/?p=1640

  8. Teddi says:

    This works great in FireFox but does not work in IE 8. It will collapse the children but not expand them back. Any ideas why?

  9. JDizzle says:

    I believe that is b/c it is using cookies. I wish there was a way to have this accordian w/o using cookies. Every other script I have found onthis site has been a hair short of fulfilling my needs. Great work nontheless!

  10. AutoSponge says:

    @Teddi,

    This is a very old version of the script using an old version of jQuery. If you are using a newer version of the library, it may not work. See a more up-to-date version here: http://www.endusersharepoint.com/2009/05/13/jquery-for-everyone-accordion-left-nav-with-cookies-speed-test/

    @JDizzle,
    This script does not use cookies. Not sure what you want it to do/not do. Removing cookie functionality only takes adding “//” in front of SetCookie in the newer script.

    It’s not difficult to take these examples and “make them your own.”

  11. webcoda says:

    Had to modify the script to get this to work in IE6 using 1.3.2:

    $(function(){
    	//initialize menus
    	    var menuRows = $("[id$='QuickLaunchMenu'] &gt; tbody &gt; tr");
    	    var menuHd = menuRows.filter("[id!='']:has(+tr[id=''])");
    	    //set img path for when submenu is hidden
    	    var closedImg = "/_layouts/images/Menu1.gif";
    	    //set img path for when submenu is visible
    	    var openedImg = "/_layouts/images/ptclose.gif";
    	    var cssInit = {
    	        "background-image": "url('"+closedImg+"')",
    	        "background-repeat": "no-repeat",
    	        "background-position": "100% 50%"
    	    }
    	    var cssClosed = {"background-image": "url('"+closedImg+"')"}
    	    var cssOpen = {"background-image": "url('"+openedImg+"')"}
    	    //hide submenus
    	    menuRows.filter("[id='']").hide();
    	    //apply initial inline style to menu headers
    	    menuHd.find("td:last").css(cssInit);
    	    menuHd.click(function (e) { 
    
    	        var styleElm = $(this).find("td:last")
    	        var nextTR = $(this).next("tr[id='']");
    	        //changed from if (nextTR.is(':visible')) {
    	        if (nextTR.is(':hidden')) {
    	            //changed from nextTR.hide();
    	            nextTR.show();
    	            //changed from styleElm.css(cssOpen)
    	            styleElm.css(cssOpen);
    	        } else {
    	            //changed from nextTR.show();
    	            nextTR.hide();
    	            //changed from styleElm.css(cssClosed)
    	            styleElm.css(cssClosed);
    	        }
    	        e.stopPropagation();
    	        return false;
    	    });
    	});
    

    Hope this saves someone some time.

  12. Ron says:

    HI Paul
    How come i cant see code for Jquery ?

    Thanks
    Ron

  13. Ron – Still working on that. We’ve got to go through each of the articles and update the code wrapper. Give us a couple days and it will be up. — Mark

  14. Ron – Should be available now. — Mark

  15. Ron says:

    Thanks Mark.
    I love the way you set up New version of endusersharepoint

  16. Walter says:

    I was wondering if there was an easy way to have all the panels open by default? Or only certain categories? This is going to be such a great addition to my site :)

  17. Hien Nguyen says:

    @autosponge

    the link http://www.endusersharepoint.com/STP/topic/dynamically-hiding-left-navigation does not work. Do you have the updated one? Thanks.

  18. Kurt says:

    Hi,
    I’m new to jQuery. How would I change the operation of the accordion so that instead of a click event activation, it would use a hover event?

    I understand I need to pass in the over and out functions into the hover function. How do I track down what element the cursor is “over” and when it is “out”?

    Is this a terribly hard thing to do in modifying the script from above? Thanks in advance for reading this.

    Kurt

  19. AutoSponge says:

    @Kurt,

    It’s not terribly hard to do, but it will take some practice. Firebug helps a lot. Read the toolbox series to get started. However, I’ll warn you that I already made a version with hover and it was terrible. Good luck.

  20. Yeva says:

    Hi, understand that this great feature is refering to the google hosted JQuery. However, my sharepoint is on SSL and with reference to the google site will cause the security prompt and depend on the user selection, this feature may not work.
    Question, is it possible to host the JQuery on my sharepoint server and reference to it? And how to I go about it ? Thanks for all the help.

  21. I have written a series of articles on how to implement local versions of jQuery in your SharePoint site:

    http://www.endusersharepoint.com/2010/01/05/build-a-sharepoint-scripting-resource-center/

Trackbacks

Check out what others are saying about this post...
  1. SharePoint Daily for December 4, 2008…

    Top News Stories Microsoft Puts Datacentres on Wheels (ZDNet) In a blog posting on Tuesday, Microsoft…

  2. [...] JQuery for Everyone: Accordion Left Nav : End User SharePoint [...]

  3. SharePoint Kaffeetasse #98…

    Tools und Addons Custom Content Editor Web Part for SharePoint jQuery http://jquery.com/ jQuery is a…

  4. OneOfSix says:

    Add Links to SharePoint Wiki Toolbar using jQuery…

  5. [...] Es muy interesante resisar el código fuente y el uso que se hace de la tecnología jQuery(un breve ejemplo de jQuery para personalizar la barra QuickLanch se puede ver en:JQuery for Everyone: Accordion Left Nav) [...]

  6. SharePoint QuickLaunchExtender…

    Codeplex sigue siendo un lugar para encontrar cosas interesantes para SharePoint:-), QuickLaunchExtender…

  7. WSS 3.0 & MOSS: Recopilatorio de enlaces interesantes (XXVI)!…

    Siguiendo con la tradición, una vez más os presentamos el clásico recopilatorio de recursos, enlaces…

  8. [...] JQuery for Everyone: Accordion Left Nav. Fuente: EndUser SharePoint.com. [...]

  9. [...] I enjoyed them all and learned a lot from our users. One of the more popular solutions, based on Accordion Left Nav and featured in Tony’s Deployment article, has only one improvement since the original [...]




Notify me of comments to this article:


Speak and you will be heard.

We check comments hourly.
If you want a pic to show with your comment, go get a gravatar!