Problem/Motivation

I can't figure out how to add column titles inside the TD rows. Can twig tweak make this easier or how can this be achieved?

I need to add "data-th='ColumnTitleHere'" to the TD per row but not sure how to make this dynamic.

In core/themes/classy/templates/views/views-view-table.html.twig it shows this:

{%
  set classes = [
    'views-table',
    'views-view-table',
    'cols-' ~ header|length,
    responsive ? 'responsive-enabled',
    sticky ? 'sticky-enabled',
  ]
%}
<table{{ attributes.addClass(classes) }}>
  {% if caption_needed %}
    <caption>
    {% if caption %}
      {{ caption }}
    {% else %}
      {{ title }}
    {% endif %}
    {% if (summary_element is not empty) %}
      {{ summary_element }}
    {% endif %}
    </caption>
  {% endif %}
  {% if header %}
    <thead>
      <tr>
        {% for key, column in header %}
          {% if column.default_classes %}
            {%
              set column_classes = [
                'views-field',
                'views-field-' ~ fields[key],
              ]
            %}
          {% endif %}
          <th{{ column.attributes.addClass(column_classes).setAttribute('scope', 'col') }}>
            {%- if column.wrapper_element -%}
              <{{ column.wrapper_element }}>
                {%- if column.url -%}
                  <a href="{{ column.url }}" title="{{ column.title }}" rel="nofollow">{{ column.content }}{{ column.sort_indicator }}</a>
                {%- else -%}
                  {{ column.content }}{{ column.sort_indicator }}
                {%- endif -%}
              </{{ column.wrapper_element }}>
            {%- else -%}
              {%- if column.url -%}
                <a href="{{ column.url }}" title="{{ column.title }}" rel="nofollow">{{ column.content }}{{ column.sort_indicator }}</a>
              {%- else -%}
                {{- column.content }}{{ column.sort_indicator }}
              {%- endif -%}
            {%- endif -%}
          </th>
        {% endfor %}
      </tr>
    </thead>
  {% endif %}
  <tbody>
    {% for row in rows %}
      <tr{{ row.attributes }}>
        {% for key, column in row.columns %}
          {% if column.default_classes %}
            {%
              set column_classes = [
                'views-field'
              ]
            %}
            {% for field in column.fields %}
              {% set column_classes = column_classes|merge(['views-field-' ~ field]) %}
            {% endfor %}
          {% endif %}
          <td{{ column.attributes.addClass(column_classes) }}>
            {%- if column.wrapper_element -%}
              <{{ column.wrapper_element }}>
              {% for content in column.content %}
                {{ content.separator }}{{ content.field_output }}
              {% endfor %}
              </{{ column.wrapper_element }}>
            {%- else -%}
              {% for content in column.content %}
                {{- content.separator }}{{ content.field_output -}}
              {% endfor %}
            {%- endif %}
          </td>
        {% endfor %}
      </tr>
    {% endfor %}
  </tbody>
</table>

How can I add something like this?:

<td data-th="{{ thTitlehere }}" {{ column.attributes.addClass(column_classes) }}>

This should display the header column titles per row in order to make the views table mobile friendly. How can I add this into the twig?

Source I am following to make table responsive: https://codepen.io/geoffyuen/pen/FCBEg
I have the table already working but on mobile view, it does not display the TH titles. In order for them to work I need to add the "data-th" to the TD row.

Any help is much appreciated.

Comments

jsidigital created an issue. See original summary.

chi’s picture

That could be something like this.
{{ column..addClass(column_classes).setAttribute('data-th', 'Title here') }}

jsidigital’s picture

Thank you for the quick reply @Chi

I had that already working.
Problem is that this adds the same static "data-th" setting to all rows and they need to be dynamic based on the table's TH column name, meaning that it displays the actual column titles per row inside this field.

For example, if my view has a table with 4 columns (Col1, Col2, Col3, Col4), the table TH should look like this:

    <th id="view-col1-table-column" class="views-field views-field-col1">Col1</td>
    <th id="view-col2-table-column" class="views-field views-field-col2">Col2</td>
    <th id="view-col3-table-column" class="views-field views-field-col3">Col3</td>
    <th id="view-col4-table-column" class="views-field views-field-col4">Col4</td>

and the table TDs should be something like this

    <td headers="view-col1-table-column" class="views-field views-field-col1" data-th="Col1">row content</td>
    <td headers="view-col2-table-column" class="views-field views-field-col2" data-th="Col2">row content</td>
    <td headers="view-col3-table-column" class="views-field views-field-col3" data-th="Col3">row content</td>
    <td headers="view-col4-table-column" class="views-field views-field-col4" data-th="Col4">row content</td>

So if I add the line you suggested:

    <td {{ column.attributes.addClass(column_classes).setAttribute('data-th', 'Title here') }}>

How do we make the 'Title here' part dynamic to show the column title?

Thank you.

chi’s picture

Instead of 'Ttitle here' pass Twig variable that contains data you needed.

jsidigital’s picture

Thank you @Chi,

I am not very familiar with advanced features of twig tweak.

I have only used it to add specific fields and such. In this case how do I add a field name or column name that is dynamic?

<td {{ column.attributes.addClass(column_classes).setAttribute('data-th', 'Title here') }}>

This code loops and applies to all TD rows and each row has a different column title.

How to get the twig variable I need if its dynamic?
The twig cheat sheet only shows specific ways of printing one specific variable, I don't see a way to print a dynamic field like I need here.

chi’s picture

That has nothing to do with Twig Tweak module. Attributes object comes from Drupal core. In order to create a variable you need some data to store in it.

jsidigital’s picture

Thank you for the help @Chi

I read up on what you mentioned as well on some twig documentation and tutorials from druplize.me and came up with the solution.

This is how I got the headers within the row loop:

<td {{ column.attributes.addClass(column_classes).setAttribute('data-th', header[key].content) }}>

So anyone that wants to make their drupal views tables responsive, now they can.

Here is the source I used and above you now know how to apply it to your views.view.table.html.twig file.
https://codepen.io/geoffyuen/pen/FCBEg

chi’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.