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 Jan 26, 2018 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, 2018 at 7:24
  • @KevN - go ahead and add this as an answer! Feb 3, 2018 at 7:49

2 Answers 2

2

I just came across this challenge too and ended up solving it slightly differently:

{% set columnsToShow = [] %}
{% set tableField = craft.app.fields.getFieldByHandle('tableFieldHandle') %}

{% for row in entry.tableFieldHandle %}
    {% for column in tableField.settings.columns|filter(column => row[column.handle] and column.handle not in columnsToShow) %}
        {% set columnsToShow = columnsToShow|push(column.handle) %}
    {% endfor %}
{% endfor %}

<table>
    <thead>
        <tr>
            {% for column in tableField.settings.columns|filter(column => column.handle in columnsToShow) %}
                <th>{{ column.heading }}</th>
            {% endfor %}
        </tr>
    </thead>
    <tbody>
        {% for row in entry.tableFieldHandle %}
            <tr>
                {% for column in tableField.settings.columns|filter(column => column.handle in columnsToShow) %}
                    <td>{{ row[column.handle] }}</td>
                {% endfor %}
            </tr>
        {% endfor %}
    </tbody>
</table>

It does the same thing as your answer, but it's a bit more concise, and this way you don't need to edit the template code if you add any new columns.

Edit to add: if you ever need to do this from the context of a table inside a matrix block instead of a simple table field, you can create the array like this, where table is the handle of the block type and you already have a reference to the current block:

{% set columnsToShow = [] %}
{% set tableColumns = block.fieldLayout.fields|filter(field => field.handle == 'table')|first.columns %}

{% for row in block.table %}
    {% for column in tableColumns|filter(column => row[column.handle] and column.handle not in columnsToShow) %}
        {% set columnsToShow = columnsToShow|push(column.handle) %}
    {% endfor %}
{% endfor %}
1

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 and acknowledge you have read our privacy policy.

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