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
Comment #2
chi commentedThat could be something like this.
{{ column..addClass(column_classes).setAttribute('data-th', 'Title here') }}Comment #3
jsidigital commentedThank 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:
and the table TDs should be something like this
So if I add the line you suggested:
How do we make the 'Title here' part dynamic to show the column title?
Thank you.
Comment #4
chi commentedInstead of 'Ttitle here' pass Twig variable that contains data you needed.
Comment #5
jsidigital commentedThank 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?
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.
Comment #6
chi commentedThat 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.
Comment #7
jsidigital commentedThank 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:
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
Comment #8
chi commented