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:
- Create a new variable, $mNamesExist, and set this to false: <<$mNamesExist=false>>.
- Use a repeating section to loop through the customers data: <<rs_customers>>…<<es_customers>>.
- Use a conditional section to check if the middle name is not blank: <<cs_{!isBlank(mName)}>>.
- Set the variable to true when a middle name is not blank: <<$mNamesExist=true>>.
- 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:
- Create a new variable called $discountTotal with an initial value of 0: <<$discountTotal=0>>.
- Use a repeating section to loop through the data: <<rs_products>>…<<es_products>>.
- 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. - 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.