Displaying Large Set of Data in a Table / Start new column after X rows
I trying to开发者_如何学Go display a large set of checkboxes in my rails app and didn't know the syntax for displaying like 15 rows then after starting a new column.
I have a model with about 120 entries. Currently, I have it being displayed in the view as....
<% for interest in Interest.find(:all) %>
<%= check_box_tag Blah Blah Blah %>
<%= interest.name %>
<% end %>
How can I make it so it makes a table and after every 15 or so rows make a new column?
It would be easiest to lay them out in rows instead of columns, because you could use each_slice:
<% Interest.find(:all).each_slice(8) do |interest_row| %>
<tr>
<% interest_row.each do |interest| %>
<td>
<%= check_box_tag Blah Blah Blah %>
<%= interest.name %>
</td>
<% end %>
</tr>
<% end %>
but if you have to them in column-major order, you can do
interest_columns = Interest.find(:all).in_groups_of(15)
interest_rows = interest_columns[0].zip(*interest_columns[1..-1]).map(&:compact)
and then do the same double loop over interest_rows
精彩评论