Conditional Columns

Category : Technical Examples

There may be times when you want to include or exclude columns in a table depending on the supplied data.

Docmosis allows tables to have conditional columns through the use of conditional fields and expressions, allowing you to determine in which instances these columns will appear in the final document.


Template
Data Files
Output Files
All Files as a ZIP

This example explained

<<cc_ denotes a conditional column in the header row of a table.

The <<cc_ is followed by a boolean field/variable or an expression. 

   <<cc_value>>   (where value is a boolean field which returns true or false)
   <<cc_$value>> (where $value is a variable which has been set to true or false)
   <<cc_{expr}>>  (where the expression enclosed in braces will return either true or false)

If the value of the condition is true, then the column will be kept.
If the value of the condition is false, then the column will be removed.


NOTE:
Irrespective of whether conditional columns are kept or removed from a table, the table will remain the same fixed width as shown in the template. If conditional columns are removed, then the remaining columns will proportionately spread out to fill the table, because the table width remains the same.


Through Boolean Fields and Expressions

Example 1
In this first example, a boolean field is used to construct the conditional Column 2 through the command <<cc_sampleValue>>.

In the supplied data file, the conditional field sampleValue is set to “false”, resulting in Column 2  being removed from the output. As a result, Columns 1 & 3 increase proportionately in width so that the overall table width remains the same as in the template.

TRY: setting sampleValue to “true”.

Example 2
This example uses an expression enclosed in braces to illustrate another way in which conditional columns can be constructed.

As the value of num is set to 3 in the supplied data, the expression <<cc_{num=3}>> returns true, which results in Column 3 being retained in the output.

TRY: setting num to a number other than 3. Notice the change to the widths of Column 1 and Column 2 in the output.

With Repeating Rows

Example 3
This example shows how conditional columns with boolean fields and expressions can be used in tables which have repeating rows.

In the supplied data, the boolean field showTrainer is set to “true” and the expression {nraces=2} returns true, meaning that both the Trainer and Race 2 Conditional Columns are kept. This in turn means that the fields <<trainer>> and <<race2>>, which are used in these columns, will be shown in the final table.

TRY: Removing these two conditional columns in the table by setting the value for the boolean field showTrainer to “false”, and the value of nraces to “1”.

 

 


LEARN MORE: Download our Docmosis Quick Reference [PDF].