本文介绍了Ruby / Rails / HTML - 创建新的表格行从循环后的X单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Rails来显示一组数据。问题是,数据是如此之大,我不真正做每个循环通常,因为我创建了这个疯狂的长列表。



我的解决方案是创建某种形式的表10个记录后创建一个新的单元格,5个单元格后创建一个新的行。我不太喜欢for循环,所以我想扔出问题了。

现在我有...

  

有一个 each_slice 方法。有了(我真的不喜欢ERB,但想法是一样的):

 %强
Person数据集:
%BR
%表
- @ persons.each_slice( 10)do | ten_people |
%tr
- ten_people.each_slice(5)do | five_people |
%td
- five_people.each do | person |
%p = person.name


I'm using Rails to display a set of data. The problem is that data is so large I dont really do the usually for each loop since I creates this insanely long list.

My solution would be to create some form of table where after 10 records create a new cell and after 5 cells create a new row. I'm not really that comfortable with for loops in rails so I figured throw the question out.

Right now I have...

<strong> Person Data Set: </strong><br />
<% for person in @persons %>
   <%= interest.name %> <br />
<% end %>

So I can I create a loop similar to this?

  <strong> Person Data Set: </strong><br />
  <table>
  <tr>
  *****for each 5 cells???? *****
  <td>
  *****For each 10 records?? ***
  </td>
  </tr>
  </table>

Has anyone had to deal with an issue like this before?

解决方案

There is an each_slice method. With HAML (I really don't like ERB but the idea is the same):

%strong
  Person Data Set:
%br
%table
  - @persons.each_slice(10) do |ten_people| 
    %tr
      - ten_people.each_slice(5) do |five_people|
        %td
          - five_people.each do |person|
            %p= person.name 

这篇关于Ruby / Rails / HTML - 创建新的表格行从循环后的X单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 08:29