All posts in QuickTip

Quick Tip – jQuery Collapsable WebParts for SharePoint 2010

jquery_collapse

Following on from the previous Quick Tip I have another cool little snippet of jQuery code to enable collapsable WebParts on SharePoint Pages.  Again like before the following code uses the OOTB v4.master so may not work if you have customised your design or used custom WebPart Control Adapters to change the formatting of WebParts.

So lets get started.  Below is the standard HTML format of how SharePoint renders a WebPart.  I have cut out unnecessary js that was in the actual source to make things cleaner.

As you can see there are alot of tables which are nested.  The table that we are interested in is this one:

This table has two rows.  The First row contains the header of the webpart which is where we are going to put our minimise handler, and the second row is the section we are going to “hide” in Javascript.  So firstly we need to inject our minimise handler into the WebPart titles <h3> tag.  To do this we need a single line of jQuery.

What this line does is firstly gets all the .s4-wpTopTable instances and within that element gets the h3 tag from within the first TR.  It then appends a custom div with some inline style to float it right with a class of “min”.  We have also used some of SharePoints built in images to reduce the number of assets required.

The next step is to now hook into the new handler that we have created above. I will create a couple of new variables to store the images that we are going to use for the collapse and expand buttons.

As you can see above i have hooked into our dynamically created handler with the class “.handler”.  When i click the item it will trigger the function and perform the following “if ” statement which i have compressed to a single line.  First i find the closest .s4-wpTopTable element which will always be the parent of the handler.  Then i want to get the first TR for that table and then go to the next row and toggle the state to be visible / hidden.  I then check the status and if it is visible then show the collapse image otherwise show the expand one.  Simple as that.

Here is the full script:

Hope this helps people to extend their SharePoint environments.  When i have written the next stage of the drag and drop piece which covers saving the WebPart states then I will update this post to include that functionality.

Chris

Quick Tip – jQuery Accordion for SharePoint 2010 Quick Launch

accordion_header

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)

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).

Next is to hide all of the children of the quicklaunch items so we are left with just the headings.

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.

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.

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.