Conditional Columns (Part 2)

The Conditional Columns article covered how to include or exclude columns in a table using Boolean fields and expressions.

However, sometimes a column may need to be excluded from a table when there is no data to display anywhere in that column. 

This can be achieved by adding a <<cc_ conditional column directive in the header row of a table and combining it with a variable that is dependent on the column's data.

Using column data

Both of the examples in the template show how the column data can be used to determine whether a column should be displayed or hidden.

In these examples, when a column is hidden, the remaining columns increase proportionally in width so that the table's overall width remains the same.

Example 1

This first example shows how to display the "Middle Name” column in a table of customers when at least one customer has a middle name.

The steps to achieve this are:

  1. Create a new variable, $mNamesExist, and set this to false: <<$mNamesExist=false>>.
  2. Use a repeating section to loop through the customers data: <<rs_customers>>…<<es_customers>>.
  3. Use a conditional section to check if the middle name is not blank: <<cs_{!isBlank(mName)}>>.
  4. Set the variable to true when a middle name is not blank: <<$mNamesExist=true>>.
  5. Use a conditional column directive <<cc_$mNamesExist>> in the table’s header row, that is based on the variable $mNamesExist.

If there are any middle names in the data, the column is shown, otherwise not. In the supplied data file, no middle name exists, so the column is hidden.

Example 2

This example shows how to display the “Discount” column in a table of products if at least one product row has a discount value. To achieve this, first add up all of the discount values, then show the column if the total discount is greater than zero.

The steps to achieve this are:

  1. Create a new variable called $discountTotal with an initial value of 0: <<$discountTotal=0>>.
  2. Use a repeating section to loop through the data: <<rs_products>>…<<es_products>>.
  3. Add the discount value to the total discount variable: <<$discountTotal ={$discountTotal+ifBlank(discount,0)}>>
    The ifBlank() function used within the expression converts the value of discount to 0 if it’s null or blank.
  4. Use a conditional column directive <<cc_{$discountTotal>0}>> in the table's header row, that is based on the variable $discountTotal.

If there are any discounts in the data, the column is shown, otherwise not. In the supplied data file, no discount exists for any of the products. This results in $discountTotal = 0, so that the "Discount" column is hidden.

Note: this method of calculating column totals can also be used to display totals in the final row of a table.
 

Feedback

Invalid Input

Sorry, this field will only accept letters and numbers, and not special characters, to limit spam.

Sorry, this field will only accept letters and numbers, and not special characters, to limit spam.

Invalid Input