Вы можете получить индекс элемента массива, используя each_with_index
, например, следующее даст вам переменную row_index
, которая начинается с 0
для первой строки:
<% @entitiquestions.in_groups_of(2, false).each_with_index do |entitiquestions, row_index| %>
Аналогично, вы можете получить индекс столбца с помощью:
<% entitiquestions.each_with_index do |entitiquestion, column_index| %>
Теперь у вас есть точное положение элемента в таблице, поэтому вы можете использовать троичный оператор для добавления класса.Например, если вы хотите добавить класс highlight
, когда строка ровная, вы можете сделать:
<span data-tooltip class="hint--bottom hint--info <%= 'highlight' if row_index.even? %>"
Вот полный пример:
<table>
<% @entitiquestions.in_groups_of(2, false).each_with_index do |entitiquestions, row_index| %>
<tr>
<% entitiquestions.each_with_index do |entitiquestion, column_index| %>
<td>
<span data-tooltip class="hint--bottom hint--info <%= 'highlight' if column_index.even? %>" data-hint="<%= entitiquestion.question.query %>">
<%= entitiquestion.question.query %>
</span>
</td>
<% end %>
</tr>
<% end %>
</table>