| | Stumble It! | Add to Mixx! | | diigo it | | Slashdot |

Wednesday, August 12, 2009

SharePoint - Expand or Collapse Web Parts v 2.0

Last November I posted some code that made it possible to expand or collapse various web parts on a SharePoint page using a little plus/minus icon. Cecilie reported some problems with that code so I decided to re-write it.

As usual, you'll need to put this code into a Content Editor Web Part at the bottom of the page.

Here's the result of that re-write:
<script type="text/javascript">
// Add the Web Part Titles here to have them opened by default
var wpsToSkip = ['Tasks','sandbox'];

function wpExpander() {
var theTDs = document.getElementsByTagName("TD");
for (var t=0;t<theTDs.length;t++) {
var id = theTDs[t].id;
if (id.match("WebPartTitleWPQ")) {
id = id.substr(id.indexOf("WPQ"));
var title = (theTDs[t].innerText || theTDs[t].textContent).replace(/[\n\r]/,'');
var strImg = "<img style='margin:6px 5px 0px 2px;cursor:hand;float:left;' ";
if (wpsToSkip.join().match(title)) {
strImg += "onClick='showHide(\""+id+"\",this)' src='/_layouts/images/minus.gif'>";
} else {
strImg += "onClick='showHide(\""+id+"\",this)' src='/_layouts/images/plus.gif'>";
document.getElementById("WebPart"+id).style.display = "none";
}
theTDs[t].innerHTML = strImg + theTDs[t].innerHTML;
}
}
}

function showHide(i,o) {
var wp = document.getElementById("WebPart"+i);
wp.style.display = (wp.style.display=="") ? "none" : "";
o.src = (o.src.match(/plus.gif/)) ? "/_layouts/images/minus.gif" : "/_layouts/images/plus.gif";
}

_spBodyOnLoadFunctionNames.push("wpExpander()");
</script>

3 comments:

mboone9 said...

Ben,
Thank you for the code.
Is there a way default-open some web parts and default-close others?
In other words, is there default-open code I could insert before/after some web parts and default-close code I could insert before after other web parts?
Thanks,
Mark

Architectural Hand Railing said...

I see the great code you wrote, but do you have an example of the relating HTML for this?

And will it work with 3 sections, where you click on one, and just that one section opens?

Then click on another section, and if one is open, it closes, and the new section opens?

Thank you

Ben said...

Hi mboone9 & AHR,

Funny that you two should post on this article as I was just yesterday updating this code. I trimmed it down a bit, made it a bit more efficient & added a a feature that lets you have some Web Parts open by default.

I've posted the updated code in the article above so you can just grab the new code from there.

You can specify which Web Parts you want to have opened by default by adding their name to the "wpsToSkip" array. In the code I posted, I have two Web Parts ("Tasks" and "sandbox") that will be left open, but still with the option to close them.

AHR,

To use this code, all you have to do is add it to a Content Editor Web Part anywhere on the page. The code will minimize every web part on the page that has a title bar unless you add the title to the "wpsToSkip" array. If you click the "+/-" sign next to the title, only that Web Part will open or close.

As for clicking on one Web Part and having the others close, stay tuned for an upcoming post that I think will better address this request.

-Ben