<p>This works for me:</p>

<pre><code>{% 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 %}


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

CC BY-SA 3.0
2018-02-04T08:54:19.267Z
2018-02-04T08:54:19.267Z
Kev N
<p>I just came across this challenge too and ended up solving it slightly differently:</p>
<pre class="lang-twig prettyprint-override"><code>{% set columnsToShow = [] %}
{% set tableField = craft.app.fields.getFieldByHandle('tableFieldHandle') %}

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

&lt;table&gt;
    &lt;thead&gt;
        &lt;tr&gt;
            {% for column in tableField.settings.columns|filter(column =&gt; column.handle in columnsToShow) %}
                &lt;th&gt;{{ column.heading }}&lt;/th&gt;
            {% endfor %}
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
        {% for row in entry.tableFieldHandle %}
            &lt;tr&gt;
                {% for column in tableField.settings.columns|filter(column =&gt; column.handle in columnsToShow) %}
                    &lt;td&gt;{{ row[column.handle] }}&lt;/td&gt;
                {% endfor %}
            &lt;/tr&gt;
        {% endfor %}
    &lt;/tbody&gt;
&lt;/table&gt;
</code></pre>
<p>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.</p>
<p>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 <code>table</code> is the handle of the block type and you already have a reference to the current <code>block</code>:</p>
<pre class="lang-twig prettyprint-override"><code>{% set columnsToShow = [] %}
{% set tableColumns = block.fieldLayout.fields|filter(field =&gt; field.handle == 'table')|first.columns %}

{% for row in block.table %}
    {% for column in tableColumns|filter(column =&gt; row[column.handle] and column.handle not in columnsToShow) %}
        {% set columnsToShow = columnsToShow|push(column.handle) %}
    {% endfor %}
{% endfor %}
</code></pre>

CC BY-SA 4.0
2023-03-14T12:50:45.920Z
2023-11-10T16:17:58.080Z
2023-11-10T16:17:58.080Z
James Smith
<p>Is there a way to conditionally display a table column heading if the table field row has been entered? i.e.</p>

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

<h2>ch1 | ch2 | ch3</h2>

<p>a   |  b  |  c</p>

<p>but, if I only fill in 2 columns without conditionals I'd see this</p>

<h2>ch1 | ch2 | ch3</h2>

<p>a   |  b  |  </p>

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

<h2>ch1 | ch2 |</h2>

<p>a   |  b  |  </p>

<p>Is that possible?</p>

CC BY-SA 3.0
2018-01-26T07:07:00.927Z
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
CC BY-SA 3.0
2018-01-27T07:24:21.027Z
Thanks yeap got this sorted like that and with some variables `{% set showCol1 = false %}` having discussed with another dev
Kev N
CC BY-SA 3.0
2018-02-03T07:49:29.233Z
@KevN - go ahead and add this as an answer!
Marion Newlevant
<conditional><table-field>
Conditionally show table column headings
# Conditionally show table column headings

(Asked by: username_0 on 2018-01-26)

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?

**Tags:** conditional, table-field

> **Comments:**
> **username_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  
> **username_0**: Thanks yeap got this sorted like that and with some variables `{% set showCol1 = false %}` having discussed with another dev  
> **username_2**: @KevN - go ahead and add this as an answer!  

---

## 2 answers

### Answer by username_0 on 2018-02-04. Score: 1 | Accepted answer

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 %}




    
        {% if showCol1 %}Col 1{% endif %}
        {% if showCol2 %}Col 2{% endif %}
        {% if showCol3 %}Col 3{% endif %}
        {% if showCol4 %}Col 4{% endif %}
        {% if showCol5 %}Col 5{% endif %}
    

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


---

### Answer by username_3 on 2023-03-14. Score: 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 %}


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


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 %}


---

