Rails循环和rowspan

时间:2017-10-26 21:07:33

标签: ruby-on-rails

我在使用条件语句的表单中的循环中遇到了rowpan的问题。我的第一个Rowspan 2在循环之外并且工作正常,但我的第二个Rowspan 2在条件内部,因此它不能正常工作。有没有办法解决这个问题?

这就是我想要实现的目标

enter image description here

表格

<table>
  <th>Header 1</th>
  <th>Header 2</th>
  <th>Description</th>
  <th>Phase</th>
  <tr>
    <td rowspan="4">Rowspan 4</td>
    <td rowspan="2">Rowspan 2</td>
    <% Identity.all.each do |identity| %>
      <%= form.fields_for :indicators, form.object.indicators.where(identity: identity).first_or_initialize do |ff| %>
        <%= ff.hidden_field :id %>
        <%= ff.hidden_field :identity_id %>
        <% if ff.object.identity.number <= 1.4 %>
          <td><%= ff.object.identity.description %></td>
          <td><%= ff.collection_select :phase_id, Phase.all, :id, :name %></td>
          </tr>
          <tr>
        <% elsif ff.object.identity.number > 1.4 %>
          <td rowspan="2">Rowspan 2</td>
          <td><%= ff.object.identity.description %></td>
          <td><%= ff.collection_select :phase_id, Phase.all, :id, :name %></td>
        <% end %>
      </tr>
    <% end %>
  <% end %>
</table>

1 个答案:

答案 0 :(得分:0)

这绝对可行,但我认为根据您对数据进行分区的方式,这对您不起作用。

具体来说,似乎你在怪异的地方关闭了,并且你在循环外创建了一个行。

更强大的解决方案可能会将数据划分为rowspan = X块(可能使用group_by,in_groups_of,chunk)

<table>
  <tr>
    <th>Header 1</th>
    <th>Header 2</th>
    <th>Description</th>
    <th>Phase</th>
 </tr>
 <tr>
   <td rowspan="4">Rowspan 4</td>
     <% Identity.all.each_with_index do |identity, index| %>
       <% if index.positive? %>
         <tr> #emit a tr in every row but the first
       <% end %>

       <% if index.even? %>
         <td rowspan="2">Rowspan 2</td>
       <% end %>

       <%= form.fields_for :indicators, form.object.indicators.where(identity: identity).first_or_initialize do |ff| %>
         <%= ff.hidden_field :id %>
         <%= ff.hidden_field :identity_id %>
         <td><%= ff.object.identity.description %></td>
         <td><%= ff.collection_select :phase_id, Phase.all, :id, :name %></td>
         </tr>  #Close a tr in every row
    <% end %>
  <% end %>
</table>

我没有对此进行过个人测试,但我认为更接近这种格式会更有效。看到你实际上要调试的html输出也很有用。