CASE: hide columns when empty

You might find yourself building a table, where some columns can hold information but not always have information in them. While that’s just fine in input-mode, in export they take in some space we might need, so what’s the point on exporting them to PDF if they don’t hold anything of information?

Well, with an IFI-statement we can do just that (more info here), but we will need to check each column separately to see if indeed the column holds at least one time data.

With below code we can do just that:

{% assign show_statement_col = false %}
{% for account in bank_accounts %}
  {% if account.custom.bank.docs.document != blank %}
    {% assign show_statement_col = true %}{% break %}
  {% endif %}
{% endfor %} 

in which we first set a variable to false to be used later in in our ifi-statement:

{% assign show_statement_col = false %}

and then loop over our collection:

{% for account in bank_accounts %}

{% endfor %}

to find if something actually has data:

{% if account.custom.bank.docs.document != blank %}

as soon as it does, we assign that variable show_statement_col to true:

{% assign show_statement_col = true %}

and stop looping over our collection with the break-tag:

{% assign show_statement_col = true %}{% break %}

We do this because it makes no sense looping through all loops if you find data already.

Now, the second column has deliberately been set without a fixed width, as the whole table has been set on 100%, so this column will take the remainder of what’s left of 100%.

A more in depth case here:

{% comment %}some needed logic{% endcomment %}
{% assign bank_accounts = #5 %}

{% comment %}check if the last columns are needed or not{% endcomment %}
{% assign show_statement_col = false %}
{% for account in bank_accounts %}
  {% if account.custom.bank.docs.document != blank %}
    {% assign show_statement_col = true %}{% break %}
  {% endif %}
{% endfor %}

{% assign show_statement_1d_col = false %}
{% for account in bank_accounts %}
  {% if account.custom.bank.docs_plus_1d.document != blank %}
    {% assign show_statement_1d_col = true %}{% break %}
  {% endif %}
{% endfor %}


{% comment %}
==================
      TABLE
==================
{% endcomment %}
{% stripnewlines %}
|----30%----
|-----------
|----15%----:
{% ifi show_statement_col %}
  |----20%----:
{% endifi %}
{% ifi show_statement_1d_col %}
  |----20%----:
{% endifi %}+
{% newline %}
|_  {% t "Account" %}                   _
|_                                 _
|_  {% t "Value" %}                 
{% ifi show_statement_col %}
  _|_  {% t "Bank statement" %}         
{% endifi %}
{% ifi show_statement_1d_col %}
  _|_  {% t "Bank statement +1" %}     
{% endifi %}                            _

{% for account in bank_accounts %}
  {% newline %}
  | {{ account.link }}
  |  
  | {{ account.value | currency_dc }}
  {% ifi show_statement_col %}
    | {% input account.custom.bank.docs as:file %}
  {% endifi %}
  {% ifi show_statement_1d_col %}
    | {% input account.custom.bank.docs_plus_1d as:file %}  
  {% endifi %}
  
  {% comment %}assign values to variable{% endcomment %}
  {% assign total_bank = total_bank+account.value %}
  
  {% if forloop.last %}
    {% newline %}
    |
    |
    |_^ {{ total_bank | currency_dc }}  ^_
  {% endif %}
{% endfor %}
{% endstripnewlines %} 
1 Like