Score:0

Ho can I add a class to a specific row in views-view-field template?

zw flag

I have created a custom views-view-field template and added some custom html and views field similar to this

<div class="{{ fields.my_text_field.content }}">
  {{ fields.my_media_field.content }}
</div>

The trouble I am having is that I want to add classes on certain rows only. Similar to below

<div class="{{ fields.my_text_field.content }} not-row-2">
  {{ fields.my_media_field.content }}
</div>
<div class="{{ fields.my_text_field.content }} is-row-2">
  {{ fields.my_media_field.content }}
</div>
<div class="{{ fields.my_text_field.content }} not-row-2">
  {{ fields.my_media_field.content }}
</div>

In the views-view template I can run a loop and check the count and add a class using code similar to below, but then this class is in a different html wrapper. Not in the same wrapper as {{ fields.my_text_field.content }}.

{% if (loop.index == 2 ) %}
  {% set rowclass = 'is-row-2' %}
{% else %}
  {% set rowclass = 'not-row-2' %}
{% endif %}
  <div class="{{rowclass}">
  {{ row.content }}
  </div>
{% endfor %} 

How can I add a class to the child (row.content) based on the row index of the parent at views-view-field. Thanks

usmanjutt84 avatar
in flag
Your question is not clear to me. Hopefully I am asking you the right question that are your trying to add a class to the wrapper of specific field in the views (e.g my_media_field)?
May  avatar
zw flag
HI @usmanjutt84, I am trying to add a class to a field based on the row it is.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.