Hide empty rows in DispForm

By Alexander

Have you ever wanted to hide rows from your DispForm if they are empty?

As always we start like this:
Create a document library to hold your scripts (or a folder on the root created in SharePoint Designer). In this example i have made a document library with a relative URL of “/test/English/Javascript” (a sub site named “test” with a sub site named “English” with a document library named “Javascript”):
IMG

The jQuery-library is found here. The pictures and the sourcecode refers to jquery-1.3.2.min. If you download another version, be sure to update the script reference in the sourcecode.

To begin with NewForm looks like this:
IMG

And your DispForm like this:
IMG

Add a CEWP below your DispForm list-form like this:
IMG

With this code:

<script type="text/javascript" src="/test/English/Javascript/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
$("td.ms-formbody").each(function(){
// Trim off all white spaces
var val = $(this).text().replace(/\s|\xA0/g,'');
// Check the string length - if it's 0 hide the field
	if(val.length==0){
		$(this).parents('tr:first').hide();
	}
});
</script>

Now your DispForm should look like this:
IMG

If you want to hide the table row based on other criteria, adapt the check in line 7 to fit your need – either it’s by length or it’s by string comparison.

Regards
Alexander

8 Responses to “Hide empty rows in DispForm”

  1. Christian Stahl Says:

    Nice, this can be useful for lists wiht many columns for ex. Thanks Alexander!
    http://www.sharepointdesigners.net

  2. Charlie Epes Says:

    Hi Alexander:
    I am using this “Hide” script on many DispForms and it’s a godsend!

    Do you know if the script can be changed to be able to print to PDF and still hide the blanks fields? If I use the browser print function, the fields are hidden but if I print to PDF, the hidden fields appear.

    Thanks-

    Charlie Epes

  3. Alexander Says:

    Hi,
    Not sure if this does any difference, but try to change line 8 like this

    (this).parents('tr:first').remove();
    

    Alexander

  4. Thiago Says:

    Hi Alexander,
    Do you know how I can hide certain fields in DispForm? Not necessarily empty fields, but fields I don’t want the public to see at all.
    Thank you!

  5. Thiago Says:

    Hi,
    That’s almost what I need, except that I don’t need the rule to be conditional. There are some fields that I don’t want them to appear at all.
    Thanks

    • Alexander Says:

      Hi,
      I trimmed off the excess code:

      <script type="text/javascript" src="/test/English/Javascript/jquery-1.3.2.min.js"></script>
      <script type="text/javascript">
      // Initiate fields to make object that can ge referenced by it's FieldInternalName
      fields = init_fields();
      
      // Arrays of fields to hide
      var arrToHide = ['YourFieldNr1','YourFieldNr2'];
      // Loop trough and hide fields
      for(i=0;i<arrToHide.length;i++){
      	$(fields[arrToHide[i]]).hide();
      }
      
      function init_fields(){
      var res = {};
      $("td.ms-formbody").each(function(){
      if($(this).html().indexOf('FieldInternalName="')<0) return;
      var start = $(this).html().indexOf('FieldInternalName="')+19;
      var stopp = $(this).html().indexOf('FieldType="')-7;
      var nm = $(this).html().substring(start,stopp);
      res[nm] = this.parentNode;
      });
      return res;
      }
      </script>
      

      Alexander

  6. Thiago Says:

    It just works! Thank you Alexander!

Leave a Reply