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>
- JQuery for Everyone: Accordion Left Nav
- JQuery for Everyone: Print (Any) Web Part
- JQuery for Everyone: HTML Calculated Column
- JQuery for Everyone: Dressing-up Links Pt1
- JQuery for Everyone: Dressing-up Links Pt2
- JQuery for Everyone: Dressing-up Links Pt3
- JQuery for Everyone: Cleaning Windows Pt1
- JQuery for Everyone: Cleaning Windows Pt2
- JQuery for Everyone: Fixing the Gantt View
- JQuery for Everyone: Dynamically Sizing Excel Web Parts
- JQuery for Everyone: Manually Resizing Web Parts
- JQuery for Everyone: Total Calculated Columns
- JQuery for Everyone: Total of Time Differences
- JQuery for Everyone: Fixing Configured Web Part Height
- JQuery for Everyone: Expand/Collapse All Groups
- JQuery for Everyone: Preview Pane for Multiple Lists
- JQuery for Everyone: Preview Pane for Calendar View
- JQuery for Everyone: Degrading Dynamic Script Loader
- JQuery for Everyone: Force Checkout
- JQuery for Everyone: Replacing [Today]
- JQuery for Everyone: Whether They Want It Or Not
- JQuery for Everyone: Linking the Attachment Icon
- JQuery for Everyone: Aspect-Oriented Programming with jQuery
- JQuery for Everyone: AOP in Action - loadTip Gone Wild
- JQuery for Everyone: Wiki Outbound Links
- JQuery for Everyone: Collapse Text in List View
- JQuery for Everyone: AOP in Action - Clone List Header
- JQuery for Everyone: $.grep and calcHTML Revisited
- JQuery for Everyone: Evolution of the Preview
- JQuery for Everyone: Create a Client-Side Object Model
- JQuery for Everyone: Print (Any) Web Part(s) Plugin
- JQuery for Everyone: Minimal AOP and Elegant Modularity
- JQuery for Everyone: Cookies and Plugins
- JQuery for Everyone: Live Events vs. AOP
- JQuery for Everyone: Live Preview Pane
- JQuery for Everyone: Pre-populate Form Fields
- JQuery for Everyone: Get XML List Data with OWSSVR.DLL (RPC)
- Use Firebug in IE
- JQuery for Everyone: Extending OWS API for Calculated Columns
- JQuery for Everyone: Accordion Left-nav with Cookies Speed Test
- JQuery for Everyone: Email a List of People with OWS
- JQuery for Everyone: Faster than Document.Ready
- jQuery for Everyone: Collapse or Prepopulate Form Fields
- jQuery for Everyone: Hourly Summary Web Part
- jQuery for Everyone: "Read More..." On a Blog Site
- jQuery for Everyone: Slick Speed Test
- jQuery for Everyone: The SharePoint Game Changer
- JQuery For Everyone: Live LoadTip
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
Is there away to ‘hide’ the Quick Launch. I would like to do away with it if possible. Hope you can help.
@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.
@Jeroen Ritmeijer,
Cool!
Thanks all, I got what I need. Have a great day :)
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.
@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
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?
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!
@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.”
Had to modify the script to get this to work in IE6 using 1.3.2:
Hope this saves someone some time.
HI Paul
How come i cant see code for Jquery ?
Thanks
Ron
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
Ron – Should be available now. — Mark
Thanks Mark.
I love the way you set up New version of endusersharepoint
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 :)
@autosponge
the link http://www.endusersharepoint.com/STP/topic/dynamically-hiding-left-navigation does not work. Do you have the updated one? Thanks.
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
@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.
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.
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/
BLOT: This is a great script, but one thing we would like to see is a little “- +” buttons just below or besides View all site content, so that a user can quickly expand all or collapse all with one click. I have seen this for a list view on a regular sharepoint list, but can not find a way to do this on the quick-launch. Does anyone else have a way or want this done also?
Background: We implemented this script and 1/2 the users love it and the other don’t like the collapsed versions and complain of how long it takes them to collapse it on every site. when they open a new Sharepoint session each day..
Nice post.
How can I achieve the below scenario:
When I click on any of the menu header box, it exposes. Again if click on the second header it should close the previous Menu header (first exposed one).
I mean to say only one menu header should expose at a time.
Please suggest…
I am looking for a way to “hide” and “unhide” the left navigation area on WSS site. I see in one of the posts above a link to http://www.endusersharepoint.com/STP/topic/dynamically-hiding-left-navigation but this link is not working. I also have a site http://vogtland.ws/MarksBlog/?p=14 that has code to put into the Content Editor web part. I tried that and including the link to my hide/unhide images in my Picture Library. But all that is showing up in the Content editor is my html code. Not really sure how that is supposed to work. Any ideas?
Mary Grace,
Put this little css markup in a Content Editor Web Part on the page you want to remove the quick launch area:
Thanks for the response but it looks like this will remove the area completely. I was interested in the other link here because that seemed like more of a user controlled dynamic option. What I want the user to be able to do is to pin or unpin the left nav.
Has anyone found a way to have certain categories close or have some remain open?
Hi,nice article you posted.it seems to be working fine.I am new to sharepoint.i have small doubt where i need to place this jquery .please could you give more explanation on this.
Hi,
I like to collapse only some item in Left Nav. In the give example , we want some thing like this , other than document navSubMenu every item should be collapsed.
Can we do this. If yes how ??