Display data in columns, not rows, using Ruby on Rails

I would like to display data in columns, not in a row on my web page.

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

Many thanks for your help!

+1
source share
4 answers

A simple solution would be to "rotate" the information using an array. Like this (pseudo) code (can't check 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 will need something like this:

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

There is, of course, the best solution, but for me it seems the simplest / most common. Rotate some data from some model.

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

+1
source

/:

def rotate(matrix)
  columns = matrix.first.size
  rows = matrix.size
  result = []
  columns.times { |y|
    result[y] = []
    rows.times { |x|
      result[y][x] = matrix[x][y]
    }
  }
  result
end

, "", 1 1 . , , array[0][0] . , , [0] [0] :

1 2 3 4
5 6 7 8
9 0 a b

1 5 9
2 6 0
3 7 a
4 8 b

, /.

, , : Array # transpose...

<%= render :partial => 'mytable', :collection => @array.transpose %> # untested
0

If you are retrieving data from a database, you should consider using the PIVOT operator (at least in SQL Server - I'm not sure about SQLLite or MySQL). That way, you can still use the “normal” user interface methods and still show the data in columns rather than rows. Just a thought ...

0
source

Here is my solution. It takes an array, say, ["a", "b", "c", "d"]and converts it to this array: [["a", "b"], ["c"], ["d"]]which is then easy to use to display data in columns.

def categories_in_columns(categories, number_of_groups=3)
  groups = []
  elements_per_column       = categories.size / number_of_groups
  extra_elements            = categories.size % number_of_groups
  has_extra_elements        = (extra_elements > 0 ? 1 : 0)

  0.upto(number_of_groups-1).each do |i|
    groups[i] = []
    while groups[i].size < (elements_per_column + has_extra_elements)
      groups[i] << categories.shift
      extra_elements -= 1 if groups[i].size > elements_per_column
    end
    has_extra_elements = 0 if extra_elements == 0
    groups[i].compact!
  end
  groups.delete_if { |i| i.empty? }
end

And the specifications for it:

it "arranges categories into columns" do
  categories_in_columns(["a", "b", "c", "d", "e"]).should == [["a", "b"], ["c", "d"], ["e"]]
  categories_in_columns(["a", "b", "c", "d"]     ).should == [["a", "b"], ["c"], ["d"]]
  categories_in_columns(["a", "b", "c"]          ).should == [["a"], ["b"], ["c"]]
  categories_in_columns(["a", "b"]               ).should == [["a"], ["b"]]
  categories_in_columns(["a"]                    ).should == [["a"]]
end
0
source

All Articles