I thought that i would share a quick piece of code that you can use on your SharePoint sites to change the Quicklaunch from a standard view to an “accordion” style version using jQuery. The following code uses the OOTB v4.master so may not work if you have customised your design. (Changes to selectors should be all that would be required to get it working with your own design)
1 |
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script type="text/javascript"> jQuery(function($) { $('.s4-ql li ul').hide(); $('.s4-ql ul li').hover(function() { $(this).find('a:first').next().slideToggle(); }, function() { $(this).find('a:first').next().slideToggle(); }).find('ul').each(function(index) { var $this = $(this); $this.parent().find('a:first .menu-item-text').append(['<span style=\'float:right;font-size:0.8em;\'>(', $this.children().length, ')</span>'].join('')); }); }); </script> |
As you can see the jQuery is pretty simple. Firstly we need to get a reference to the jQuery library. (we have the version hosted with Microsoft. If you have a server which does not have internet access, you will need to download and reference a locally stored version of the library).
1 |
<script type="text/javascript" src="http://ajax.Microsoft.com/ajax/jQuery/jquery-1.7.1.min.js"></script> |
Next is to hide all of the children of the quicklaunch items so we are left with just the headings.
1 |
$('.s4-ql li ul').hide(); |
The Next step is to capture when the user hovers over the headings. When they do this they need to find the first anchor tag in the current <li> element and for the next element in the DOM (which is the UL) perform a slide toggle which will animate the accordion. When you hover the mouse off the item the alternate function is run the same code again which will reverse the accordion.
1 |
$('.s4-ql ul li').hover(function() { $(this).find('a:first').next().slideToggle(); }, function() { $(this).find('a:first').next().slideToggle(); }) |
The final step is to add a count of the number of children into each of the headings. This will provide the end user with an indicator telling them an item as some hidden items. So with the current child <ul> we need to get the parent item (the heading) and get the text inside the anchor tag. To this anchor tag we append the number of items using the $this.children().length code block.
1 |
.find('ul').each(function(index) { var $this = $(this); $this.parent().find('a:first .menu-item-text').append(['<span style=\'float:right;font-size:0.8em;\'>(', $this.children().length, ')</span>'].join('')); }); |
Finally you need to insert this into your masterpage using SharePoint Designer and thats it. I hope this quick tip was useful. I will hopefully be adding some more soon.
Please let me know how you get on.
to achieve this you would have to use cookies to save the state of the page or adjust the jquery to check the contents and if there is a child element found then expand the page. With different quick launches common on different sites i do not know if this solution would work?
This is something that i am planning on doing soon but there are many things im trying to get completed. Watch this blog soon and you should have your solution.
Thanks
Chris
Hi
Is it possible to use jquery to set the width of the quick launch area? I am using metadata navigation pane which I resize manually but I’d like to set the width or have it automatically resize?
Mike
It is possible the subitems are displayed with hover, can be done with click, the client needs it in that way.
THX for you possible response…
Yes you can use jQuery. The width is defined in css which you also can override therefore don’t need to use jquery to accommodate this requirement
The updated version of the accordion should provide this functionality. http://www.lifeinsharepoint.co.uk/2012/05/23/quick-tip-jquery-click-expand-collapse-quicklaunch/