0

I'm trying to find a way to display an array in a Rails view as follows:

 [  1  ] [  4  ] [  7  ] [  10  ]
 [  2  ] [  5  ] [  8  ] [  11  ]
 [  3  ] [  6  ] [  9  ] [  12  ]

In my controller, I have an array defined as

 def index
 @myArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
 end

Since this display order is not typical for a table, I am unsure how to proceed to accomplishing this.

I would like to know the following:

  1. What's the best way to display the values (above) in a table on my Rails view page using the order that I have requested in the code? Simply looping over the @myArr with <tr> and <td> results in the following:

    [  1  ] [  2  ] [  3  ] [  4  ]
    [  5  ] [  6  ] [  7  ] [  8  ]
    [  9  ] [  10 ] [  11 ] [  12 ]
    

Thanks in advance for your help!

4

1 回答 1

3

如果你想用 Ruby 来做,那么:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12].each_slice(3).to_a.transpose.flatten
# => [1, 4, 7, 10, 2, 5, 8, 11, 3, 6, 9, 12]

但我不会这样做。我会使用 CSS3flexbox属性来做到这一点。

于 2013-06-24T05:27:31.423 回答