1

Is there a way to conditionally display a table column heading if the table field row has been entered? i.e.

if my table has up to 3 columns and I fill in ALL the fields like this I'd see

ch1 | ch2 | ch3

a | b | c

but, if I only fill in 2 columns without conditionals I'd see this

ch1 | ch2 | ch3

a | b |

I'd like to conditionally only show the column headings/rows populated like this:

ch1 | ch2 |

a | b |

Is that possible?

3
  • 1
    Can't you just loop through your table and check if the rows are set and if not you don't display the headline? That's what we do in this case – Robin Schambach Jan 26 '18 at 7:07
  • Thanks yeap got this sorted like that and with some variables {% set showCol1 = false %} having discussed with another dev – Kev N Jan 27 '18 at 7:24
  • @KevN - go ahead and add this as an answer! – Marion Newlevant Feb 3 '18 at 7:49
0

This works for me:

{% set showCol1 = false %}
{% set showCol2 = false %}
{% set showCol3 = false %}
{% set showCol4 = false %}
{% set showCol5 = false %}


{% for row in entry.fieldname %}
{% if row.col1|length %}{% set showCol1 = true %}{% endif %}
{% if row.col2|length %}{% set showCol2 = true %}{% endif %}
{% if row.col3|length %}{% set showCol3 = true %}{% endif %}
{% if row.col4|length %}{% set showCol4 = true %}{% endif %}
{% if row.col5|length %}{% set showCol5 = true %}{% endif %}
{% endfor %}


<table>
<thead>
    <tr>
        {% if showCol1 %}<th>Col 1</th>{% endif %}
        {% if showCol2 %}<th>Col 2</th>{% endif %}
        {% if showCol3 %}<th>Col 3</th>{% endif %}
        {% if showCol4 %}<th>Col 4</th>{% endif %}
        {% if showCol5 %}<th>Col 5</th>{% endif %}
    </tr>
</thead>
{% for row in entry.fieldname %}
    <tr>
        {% if showCol1 %}<td>{{ row.col1 }}</td>{% endif %}
        {% if showCol2 %}<td>{{ row.col2 }}</td>{% endif %}
        {% if showCol3 %}<td>{{ row.col3 }}</td>{% endif %}
        {% if showCol4 %}<td>{{ row.col4 }}</td>{% endif %}
        {% if showCol5 %}<td>{{ row.col5 }}</td>{% endif %}
    </tr>
{% endfor %}
</table>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.