2017-10-26 17 views
0

条件文を使用してフォーム内のループ内でrowspanに問題があります。私の最初の行2はループの外にあり、正常に動作しますが、私の2番目の行2は条件内にあるため、動作しません。これを解決する方法はありますか?Rails loopとrowspan

は、これは私が

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> 

答えて

0

フォームは、これは間違いなくなんとかですが、私はどのように基づいて、それはあなたのために働いていないと思いますあなたはデータを分割しています。

特に、奇妙な場所でtrを閉じているように見えるだけでなく、ループの外側に1つの行スパンを作成するようです。

より堅牢なソリューションは、おそらく(チャンク、in_groups_of、多分GROUP_BYを使用して)ROWSPAN =のXブロックにデータを分割します

<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出力を見ると便利です。