There may be times when you want to include or exclude columns in a table depending on the supplied data.
Docmosis allows tables to conditionally add or remove a column through the use of conditional fields and expressions, allowing you to control in which cases a column should appear in the final document.
The syntax <<cc_ denotes a conditional column field. This must appear in the first row of a table, in the column that will be added or removed.
The <<cc_ should be followed by either a boolean field, or a variable or an expression.
<<cc_example>> | where example is a boolean provided in the data with a value of true or false |
<<cc_$value>> | where $value is a variable which has been set to true or false using an expression earlier in the template |
<<cc_{expr}>> | where the expression, enclosed in braces, evaluates to either true or false |
If the value is true, then the column will remain.
If the value is false, then the column will be removed.
NOTE: Irrespective of whether conditional columns are kept or removed from a table, the table will always remain the same fixed width as in the template. If columns are removed, then the remaining columns will proportionately spread out to fill the table, and the overall table width remains the same.
Through boolean fields and expressions
Example 1
In this first example, a data item that contains a boolean true/false value is used to control the addition/removal of Column 2 through the command <<cc_sampleValue>>.
In the supplied data, the value of sampleValue is set to “false”, resulting in Column 2 being removed from the output. As a result, the width of Columns 1 & 3 increase proportionately so that, overall, the table width remains the same as in the template.
Try setting sampleValue to “true”.
Example 2
This example uses an expression enclosed in braces, that evaluates to a true/false value, 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”.