Hiding (Empty) fields

Friday, January 25th, 2008

There are different ways to hide fields on a form.
In this post I will start with discussing two different possibilities.
The first completely hides the field and the space below the field. The form will become smaller if field are hidden. (field on the right will move to the left if the field on the left is hidden).
version 1
The first only hides the field and the label. The space and form layout stays the same.

if (crmForm.all.new_attribute.DataValue == null);
{
crmForm.all.new_attribute_c.style.visibility = 'hidden'
crmForm.all.new_attribute_d.style.visibility = 'hidden'
}

//(the _c.style is for the label and the _d.style is for the field.)

To show the field again:

crmForm.all.new_attribute_c.style.visibility = 'visible'
crmForm.all.new_attribute_d.style.visibility = 'visible'

version 2
The second completely hides the field and the space below the field. The form will become smaller if field are hidden. (field on the right will move to the left if the field on the left is hidden).

var Disabled = (crmForm.all.new_attribute.DataValue == null);
var displayStyle = Disabled ? "none" : "";
crmForm.all.new_attribute_c.style.display = displayStyle;
crmForm.all.new_attribute_d.style.display = displayStyle;

or

if (crmForm.all.new_attribute.DataValue == null);
{
crmForm.all.new_attribute_c.style.display = 'none';
crmForm.all.new_attribute_d.style.display = 'none';
}
else
{
crmForm.all.new_attribute_c.style.display = 'block';
crmForm.all.new_attribute_d.style.display = 'block';
}

//(the _c.style is for the label and the _d.style is for the field.)

A little demonstration:
Original form:
original form

The field that will be hidden if empty are: accountnumber, parentaccountid, fax, emailaddress1 and customertypecode.

With version 1 the form will look like this:
version 1

With version 2 the form will look like this:
version 2

The code for version 2 I have used is:

if (crmForm.all.accountnumber.DataValue == null);
{
var Disabled = (crmForm.all.accountnumber.DataValue == null);
var displayStyle = Disabled ? "none" : "";
crmForm.all.accountnumber_c.style.display = displayStyle;
crmForm.all.accountnumber_d.style.display = displayStyle;
}
if (crmForm.all.parentaccountid.DataValue == null);
{
var Disabled = (crmForm.all.parentaccountid.DataValue == null);
var displayStyle = Disabled ? "none" : "";
crmForm.all.parentaccountid_c.style.display = displayStyle;
crmForm.all.parentaccountid_d.style.display = displayStyle;
}
if (crmForm.all.fax.DataValue == null);
{
var Disabled = (crmForm.all.fax.DataValue == null);
var displayStyle = Disabled ? "none" : "";
crmForm.all.fax_c.style.display = displayStyle;
crmForm.all.fax_d.style.display = displayStyle;
}
if (crmForm.all.emailaddress1.DataValue == null);
{
var Disabled = (crmForm.all.emailaddress1.DataValue == null);
var displayStyle = Disabled ? "none" : "";
crmForm.all.emailaddress1_c.style.display = displayStyle;
crmForm.all.emailaddress1_d.style.display = displayStyle;
}
if (crmForm.all.customertypecode.DataValue == null);
{
var Disabled = (crmForm.all.customertypecode.DataValue == null);
var displayStyle = Disabled ? "none" : "";
crmForm.all.customertypecode_c.style.display = displayStyle;
crmForm.all.customertypecode_d.style.display = displayStyle;
}

To hide a complete section on a form:
Choose one of the fields in the section (new_attribute) and use this code:

crmForm.all.new_attribute_c.parentElement.parentElement.parentElement.style.display='none'
//To show the section again:
crmForm.all.new_attribute_c.parentElement.parentElement.parentElement.style.display='block'

Hide a complete section on a form

Wednesday, January 16th, 2008

To hide a complete section on a form.
Choose one of the fields in the section (new_attribute) and use this code:

crmForm.all.new_attribute_c.parentElement.parentElement.parentElement.style.display = 'none'

To show the section again:

crmForm.all.new_attribute_c.parentElement.parentElement.parentElement.style.display = 'block'