Display data in columns not rows using Ruby on Rails

前端 未结 4 1126
慢半拍i
慢半拍i 2020-12-10 10:01

I would like to display data in Columns rather than Rows in my web page.

What is the most efficient way to do this using Ruby on Rails?

Many thanks for your

4条回答
  •  甜味超标
    2020-12-10 10:12

    Simple solution would be to 'rotate' the information, using an array. Like this (pseudo) code (cannot check if atm)
    Controller code:

    @stuffs = Stuff.find(:all)
    @rotated = []
    @rotated[0] = [] # Stuff column0
    @rotated[1] = [] # Stuff column1
    # etc
    index = 0
    # Now put the stuff in an array for easy(ier) access
    @stuffs.each do |stuff|
      @rotated[0][index] = stuff.detail0
      @rotated[1][index] = stuff.detail1
      index += 1
    end
    

    In your View you'd need something like this:

    
    Table head here!
    <% 2.times do |row| %>  # Build table (we have 2 stuff details, hence the 2 here)
      
      <% index.times do |column| %>
        
      <% end %>
      
    <% end %>
    
    <%= @rotated[row][column] %>

    There are of course better solution, but this seems the most simple/general to me. To rotate some data from some Model.

    Like others said with more information we can help you probably much better!

    提交回复
    热议问题