SharePoint form: present fields side-by-side


I got this request from Khan:

Alexander,
… Another question I have is how I can display two fields (and their labels) in the same row? I know I am asking too much. But I really hope you can give me some advice o this. Thanks man.

Here is an image of a few random fields put side-by-side:
IMG

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.

Add a CEWP below your NewForm/DispForm/EditForm with this code:

<script type="text/javascript" src="/test/English/Javascript/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="/test/English/Javascript/FieldsSideBySide.js"></script>
<script type="text/javascript">
  // Append fields to the field "User"
  arrToPutSideBySide = ['StartTime','EndTime'];
  customSideBySide(arrToPutSideBySide,'User');
  // Appends another set of fields to the field "User"
  arrToPutSideBySide = ['FirstName','MiddleName','LastName'];
  customSideBySide(arrToPutSideBySide,'User');

  // To append the fields to the top use this code:
  //arrToPutSideBySide = ['FirstName','MiddleName','LastName'];
  //customSideBySide(arrToPutSideBySide,'',true);
</script>

This code is not the full code from the screenshot above. Each horizontal “section” is made with a separate call to the function “customSideBySide()”.

Parameters explained:

  • arr: Array or fields to put in one line.
  • insertAfterField: If you want to append the fields below another field, insert the FieldInternalName here. Set this parameter to “” if you want to use the next parameter.
  • appendToTop: To insert the fields in the top of the form, set it to true. To insert at the bottom of the form, set it to false.

The sourcecode for the file “FieldsSideBySide.js” looks like this:

/* Sharepoint form: Present fields side-by-side
 * ---------------------------------------------
 * Created by Alexander Bautz
 * alexander.bautz@gmail.com
 * http://sharepointjavascript.wordpress.com
 * v1.0
 * LastMod: 26.01.2010
 * ---------------------------------------------
 * Must include reference to:
 *  jQuery - http://jquery.com
 * ---------------------------------------------
*/

function customSideBySide(arr,insertAfterField,appendToTop){
if(typeof(fields)=='undefined')fields = init_fields();
// Does the field specified as "insertAfterField" exist?
if(insertAfterField!='' && typeof(fields[insertAfterField])=='undefined'){
	alert("The FieldInternalName \"" + insertAfterField + "\", which is specified as \"insertAfterField\" does not exist.");
	return false;
}
	var str = '';
	$.each(arrToPutSideBySide,function(i,fin){
		// Check if FieldInternalName is correct
		if(typeof(fields[fin])=='undefined'){
			alert("The FieldInternalName \"" + fin + "\" does not exist.");
			return false;
		}
		// Adapt width of single line and multi lines of text
		if($(fields[fin]).html().indexOf('FieldType="SPFieldText"')>0){
			$(fields[fin]).find('input').css({'width':'100%'});
		}else if($(fields[fin]).html().indexOf('FieldType="SPFieldNote"')>0){
			$(fields[fin]).find('textarea').css({'width':'100%'});
		}
		var tdWidth = Math.round(100/arr.length);
		str += "<td class='ms-formbody' style='width:"+tdWidth+"%'>" + $(fields[fin]).find('.ms-formlabel').html() + $(fields[fin]).find('.ms-formbody').html() + "</td>";
		// Remove original field
		$(fields[fin]).remove();			
	});
	str = "<tr><td colspan='2'><table cellpadding='0' cellspacing='0' style='width:100%'><tr>" + str + "</tr></table></td></tr>";	
	if(insertAfterField!=''){
		$(fields[insertAfterField]).after(str);
	}else{
		if(appendToTop){
			$("table.ms-formtable tbody:first").prepend(str);
		}else{
			$("table.ms-formtable tbody:first").append(str);
		}
	}
}

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

Save as “FieldsSideBySide.js” – mind the file extension, and upload to the script library as shown above.

Ask if something is unclear.

Regards
Alexander

64 Responses to “SharePoint form: present fields side-by-side”

  1. John Brooks Says:

    Does anyone know how to get the side by side code to work on a multiline enhanced text field? Thanks

    • Alexander Bautz Says:

      Hi,
      This is not possible due to the complexity of the rich text fields.

      Alexander

  2. Roger E. Says:

    I cannot get this fieldssidebyside.js to work with Firefox. It works in IE – Can you provide assistance?

  3. April M. Says:

    This is working great with the exception of date fields. The calendar pop-up is not working. I am not getting any errors, the pop-up just does not display Did anyone else run into this issue? If so, how did you resolve?

  4. Bob Mac Says:

    This will not work on a custom form as there is no FiledInternalName…

    The function init_fields needs to be flexible to accomodate this i.e.

    function init_fields(type)

    • Alexander Bautz Says:

      There is no way you could have a function that identifies fields in all customized forms. The fact that the form is customized by individual users makes it impossible to span them all in one function. If you read the topmost note on the front page of this site, I have given an example on how you can make your own custom “init_fields” function.

      Alexander

    • Bob Mac Says:

      Ahh, thanks. I didn’t see that.

  5. Brian Says:

    I am receiving the following message: “The FieldInternalName “User”, which is specified as “insertAfterField” does not exist.

    Any ideas?

    Thanks,
    Brian

    • Alexander Bautz Says:

      Are you sure the correct FieldInternalName is “User”?

      Look here for tips on getting the FieldInternalName

      Alexander

    • Brian Says:

      Thanks Alexander! I will review it. Another Question? I have a list that has over 200 columns and when viewing and editing the contents of the list, I have to scroll through the entire list to review fields that have information in them traversing fields that are blank. The fields are unique for each record and may be populated depending on the record so hiding them is not an option. Is there a way without using SharePoint Designer to dynamically sort fields on a list on New, Edit, and Disp form that have content in them for each unique record?

      Your feedback is greatly appreciated.

      Regards,
      Brian

    • Alexander Bautz Says:

      Hi,
      I’m not sure I understand. What do you mean by “each unique record”? How would the script know which fields to show or hide?

      Please answer by adding a new comment (with the full explanation) in the requests-page

      Alexander

  6. Brian Says:

    Hi Alexander,

    First, I got the side by side functionality to work. It was the “Field Internal Name”. Thanks! I have a list that has a large number of records and some of those records do not have values in all of the columns. Some of the fields are blank which is why I called them unique. I am importing a spreadsheet into SharePoint as a list. The data is an output from another system. The issue is that each record is too long to scroll through. When I display a record or edit I have to scroll down through all of these fields in the record that some of them are blank. I am only interested in seeing the ones with content in them. I can’t hide them because while one record may have a column that is empty it maybe populated in another record. I am looking to better display the list record when viewing or editing by not having to scroll an entire record for information. I want the ability to show the record with fields that have content first then the blank fields to appear after them on a sharepoint list.

    Thanks,
    Brian

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

Join 435 other followers