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!

1 Answer 1

3

If you want to do it in Ruby, then:

[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]

But I wouldn't do it this way. I would use CSS3 flexbox properties to do it.

Sign up to request clarification or add additional context in comments.

1 Comment

Sawa, can you expand on how you would do this with CSS3 properties? Thanks in advance. I appreciate it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.