Search code examples
javascriptsharepoint-2010

Hiding List View web parts when they are empty (SharePoint)


Searching on Google, I found this code. I need to include this script in my page to hide empty Lists. How can I do this?

<script type="text/javascript">  
function HideEmptyWebParts()  
{  
var itemsfound = new Array;  
var elements = document.getElementsByTagName('*');  
for(var i=0;i<elements.length;i++)  
{  
  if(elements[i].className == 'ms-vb')  
  {  
     itemsfound.push(elements[i]);  
  }  
}  

for (var i=0;i<itemsfound.length;i++)  
{  
    if (itemsfound[i].innerHTML.indexOf("There are no items to show in this view of the")>-1)  
    {  
            itemsfound[i].parentNode.parentNode.parentNode.parentNode.innerHTML="<div class='ms-      vb'>Not enough data to produce a dashboard.</div>";  

      }  
  }  
}  

_spBodyOnLoadFunctionNames.push("HideEmptyWebParts")  

</script>  

Solution

  • Put it in master page if you want to apply this in whole site. or reference it. But if you want to apply this in a specific page, add content editor and paste it in html.