Friday, March 25, 2011

Hide SharePoint List Fileds with blank value from Display form, using HTML comments

<script language="javascript" type="text/javascript">
_spBodyOnLoadFunctionNames.push("Fields");
function FindField(fieldName) {
   // get all the HTML comments
   var arr = document.getElementsByTagName("!");
  
   for (var i=0;i < arr.length; i++ )
   {
      // check the field name
      if (arr[i].innerHTML.indexOf(fieldName) != -1)
          return arr[i];     
        
   }
}
//implimenation of string trim function
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };
function Fields() {
   var field = FindField("All Day Event");
    HideField(field);
 field = FindField("Recurrence");
 HideField(field);
 field = FindField("Workspace");
 HideField(field);
}
function HideField(field)
{
 if( field!= null)
 {

 
  var childNods = field.parentNode.childNodes;
  for( var j=0; j< childNods.length;j++)
  {
   if(childNods[j].innerHTML != null)
    {
     var innerCode = field.parentNode.innerHTML;
     var cmtCode = childNods[j].innerHTML;
     var cmtEnd = innerCode.lastIndexOf("-->");
     var childCmtEnd = cmtCode.lastIndexOf("-->");
    
      if(childCmtEnd > 0)
      {
      
       var len=   innerCode.length;
       var fieldValue = innerCode.substring(cmtEnd + 3, len);
        if(fieldValue != null)
       {
        fieldValue = fieldValue.replace("&nbsp;","");
      
        fieldValue = fieldValue.trim();
         if(fieldValue == "" )
           field.parentNode.parentNode.style.display="none";
    
        }
      }
      else
      {
     if(childNods[j].outerHTML.indexOf("<IMG") != -1)
     continue;
    
     var otherThanCommentTag = childNods[j].innerHTML;
      otherThanCommentTag = otherThanCommentTag.replace("&nbsp;","");
      otherThanCommentTag = otherThanCommentTag.trim();
      otherThanCommentTag= otherThanCommentTag.replace(/<(.|\n)*?>/g,"");
 
      if(otherThanCommentTag == "" )
           field.parentNode.parentNode.style.display="none";
      
      }
 
   }
 }
 
}
}
</script>

No comments: