11/03/2009

SharePoint: Hide a web part with zero rows

 

(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:

Anonymous said...

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?

Mike Smith said...

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

Italiano said...

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.

Mike Smith said...

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

Italiano said...

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.

Ben said...

None of my webparts on the page have a "summary" property.

Mike Smith said...

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

QQ said...

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

Anonymous said...

SWEET! thank you for this - only thing it's not working with for me is announcements.... but I can live with that :) - THANKS!

Peter van der Smitte said...

BRILLIANT!

Exactly what I was looking for!

Thank you.

Anonymous said...

Excellent! Just what I needed.

not2technical said...

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?

Mike Smith said...

not2technical,

Which list types did it work on? And which did it not work on?

Also, SharePoint 2007 or 2010?

Mike

Josh Henninger said...

Can this be applied to SharePoint 2010? I was unsuccessful when attempting it.

Alan said...

I'd also like to know if this can be ported to SharePoint 2010.

Mike Smith said...

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

Phil said...

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();
});

Note to spammers...

Spammers, don't waste your time... all posts are moderated. If your comment includes unrelated links, is advertising, or just pure spam, it will never be seen.