(I wrote this up a few months ago, and then forgot to post it. So here it is finally!)
Here’s a JavaScript trick to hide a list or library web part when there are no items in the list, or the current view used in the web part.
Steps:
- On the same page as your web part, add a Content Editor Web Part (CEWP)
- Select Edit and then Modify Shared Web Part
- In the Advanced section set the Chrome to none
- Click the Content Editor button and add the JavaScript from below
- Replace "Test List" with your web part's name
(If you don’t know the name or your guess does not work, view the HTML source of the page (View, Source in IE) and do a search for “summary=”.)
Note that the commented out section can be used to just hide the title bar instead of the entire web part.
<script> function HideWebPartWithZeroRows() { var a = document.getElementsByTagName("TABLE") for (var i=0;i<a.length;i++) { if (a[i].summary=="Test List") { if (a[i].rows.length==1) { //hide all but the title bar //var x = a[i].parentNode.parentNode.parentNode.parentNode.parentNode //hide the entire web part var x = a[i].parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode x.style.display="none"; } } } } _spBodyOnLoadFunctionNames.push("HideWebPartWithZeroRows") </script>
.
17 comments:
When I use this code, it hides the entire column (where I have another Content Editor Web Part) instead of just the web part with the empty list. Any ideas?
Anonymous,
I should only hide the one empty web part. If that web part is the only one in the column, then the column might change width.
What you might try is to set the width property of the Content Editor Web Part to hold open the width of the column.
Mike
I used the code as shown, just changed web part name and it will only hide the web part when I have one entry. Zero entries or 2 or more and it shows.
Italiano,
You may be using a web part I have not tested with, or you may have set Chrome to None. In any case find this line:
if (a[i].rows.length==1)
and change the 1 to a 0 and see if it works.
Mike
Thanks Mike, I am not sure what was happening, but it is working now. Thanks and thanks for the easy solution! This will come in very handy.
None of my webparts on the page have a "summary" property.
Ben,
> None of my webparts on the page have a "summary" property.
Which web parts do you have? The sample code should work on all web parts in the "Lists and Libraries" section of Add a Web Part.
Let me know which web part and I'll see what needs to be changed.
Mike
For some reason this doesn't work with Tasks for me. It used to, but then I changed the view to do a bit more filtering, and now even if empty it still returns rows==2..
SWEET! thank you for this - only thing it's not working with for me is announcements.... but I can live with that :) - THANKS!
BRILLIANT!
Exactly what I was looking for!
Thank you.
Excellent! Just what I needed.
I have several lists I want to apply this to. Actually all lists on the page. I copied and duplicated the "if (a[i].summary==..." section and added the list names, one in each section. Unfortunately it works on 2 lists and not on two others. I copied and pasted the list names from the web page and SharePoint Developer. Any ideas?
not2technical,
Which list types did it work on? And which did it not work on?
Also, SharePoint 2007 or 2010?
Mike
Can this be applied to SharePoint 2010? I was unsuccessful when attempting it.
I'd also like to know if this can be ported to SharePoint 2010.
Alan and Josh,
Yes, it will work with 2010, with changes. In my book I have three versions of the JavaScript, one to hide a single empty web part, one to hide any number of empty web parts and one to hide all empty web parts.
I'll put adding this to the blog on my to-do list... it may be week or two.
Mike
I never saw the jQuery version of this, I am starting to learn it, figured I would give a little back :). Bit of a different take with jQuery, it searches for the "no items" text and hides the closest "MSOZoneCell", so it should work with any web part that displays the text. This would need the jQuery library loaded and script tags around it:
$(document).ready(function(){$("td .ms-vb:contains('There are no items to show in')").closest('td[id^="MSOZoneCell_WebPart"]').hide();
});
Post a Comment