如何使用循环和 haml 和 ruby​​ 创建一个表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5521757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:50:16  来源:igfitidea点击:

how do I create a table using loops and haml with ruby?

ruby-on-railshaml

提问by sybind

I'm trying to make an html table that looks like this:

我正在尝试制作一个如下所示的 html 表格:

1 2 3 4 5
1 2 3 4 5
1 2 3 4 5

My data structure is like this: @f_ary = [ 1..250]

我的数据结构是这样的:@f_ary = [ 1..250]

Here's my haml code:

这是我的haml代码:

%table{:border => "1"}
  %tbody
    %tr 
      - cnt = 0 
      - @f_ary.each do |f| 
        - cnt += 1
        %td= cnt 
        - if cnt == 5
          - cnt = 0 
          %tr 

My current output looks like this:

我当前的输出如下所示:

<table border='1'>
  <tbody>
    <tr>
      <td>1</td>
      <td>2</td>
      <td>3</td>
      <td>4</td>
      <td>5</td>
      <tr></tr>
      <td>1</td>
      <td>2</td>
      <td>3</td>
      <td>4</td>
      <td>5</td>
    </tr>
  </tbody>
</table>

I want it to look like this:

我希望它看起来像这样:

<table border='1'>
  <tbody>
    <tr>
      <td>1</td>
      <td>2</td>
      <td>3</td>
      <td>4</td>
      <td>5</td>
    </tr>
    <tr>
      <td>1</td>
      <td>2</td>
      <td>3</td>
      <td>4</td>
      <td>5</td>
    </tr>
  </tbody>
</table>

回答by Todd Yandell

You should try to put all the logic for creating the rows and columns array in your controller. Rendering the view in Haml then becomes very simple:

您应该尝试将用于创建行和列数组的所有逻辑放在您的控制器中。在 Haml 中渲染视图变得非常简单:

Controller:

控制器:

@items = [
  [1,  2,  3,  4,  5],
  [6,  7,  8,  9,  10],
  [11, 12, 13, 14, 15]
]

View:

看法:

%table
  %tbody
    - @items.each do |row|
      %tr
        - row.each do |column|
          %td= column

If you have a flat array of items rather than an array of arrays as in my example, you can easily convert it with flat_array.each_slice(5).to_a, where 5is the number of columns.

如果您有一个平面数组而不是我的示例中的数组数组,您可以轻松地将其转换为flat_array.each_slice(5).to_a,其中5是列数。

回答by James Brennan

You can use the each_slice like so:

您可以像这样使用 each_slice:

- @f_ary.each_slice(5) do |row|
  %tr
    - row.each do |cnt|
      td=cnt

回答by sameera207

update

更新

This could be the most un-ruby way of doing it, (I did this around 3 years back). So

这可能是最不红的方式,(我大约在 3 年前就这样做了)。所以

check out above answers & they are much more better

看看上面的答案 & 他们更好

I will just keep this answer without deletion, just as a reference on, how NOT to do it... ;)

我只会保留这个答案而不删除,作为参考,如何不这样做......;)



Have an internal counter in the view, when it comes to 5, add a . psudo will look some thing like this

在视图中有一个内部计数器,当涉及到 5 时,添加一个 . psudo 看起来像这样

couneter = 0 

@items.each |item|
   if counter == 0
    <tr>
   end
   if counter != 5
     <td>item</td>
     counter ++
   end
   if counetr == 5
     </tr>
     counetr = 0
   end 

end


end

I hope u get the idea

我希望你明白

cheers

干杯

sameera

萨梅拉