Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-04T08:54:19.267Z
Answers.LastActivityDate: 2018-02-04T08:54:19.267Z
Answers.UserDisplayName: Kev N
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-14T12:50:45.920Z
Answers.LastActivityDate: 2023-11-10T16:17:58.080Z
Answers.LastEditDate: 2023-11-10T16:17:58.080Z
Answers.UserDisplayName: James Smith
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-01-26T07:07:00.927Z
Comments.Text: 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
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-01-27T07:24:21.027Z
Comments.Text: Thanks yeap got this sorted like that and with some variables `{% set showCol1 = false %}` having discussed with another dev
Comments.UserDisplayName: Kev N
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-02-03T07:49:29.233Z
Comments.Text: @KevN - go ahead and add this as an answer!
Comments.UserDisplayName: Marion Newlevant
Tags: <conditional><table-field>
Title: Conditionally show table column headings
ThreadText: # 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**: @username_0 - 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 %}
  
  <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>
  
  ```
  
  
  ---
  
  ### Answer by username_3 on 2023-03-14. Score: 2
  
  I just came across this challenge too and ended up solving it slightly differently:
  
  ```twig
  {% 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`:
  
  ```twig
  {% 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 %}
  
  ```
  
  
  ---
  
  