Transpose html table

Is it possible to transpose an html table (without javascript). I am creating a table with rails (and erb) from a list of objects. So this is really easy and natural to do when each line corresponds to one object. However, I need each object to appear as a column. I would like to have only one loop and describe each column, rather than doing the same loop for each column. (This does not have to be a real table, it can be a list or anything that does the trick).

Update

To clarify the issue. I don't want to transpose the array in ruby, but to display an html table with a vertical row. My actual table actually uses one part for each row that generates a cell list (td). It could be a change in the list if that helps. In any case, this is an HTML question, not a ruby ​​one: how to display a table with rows vertically (and not horizontally).

+4
source share
2 answers

Apparently the answer is no: - (

0
source

You may need something like this?

class Array
  def transpose
    # Check here if self is transposable (e.g. array of hashes)
    b = Hash.new
    self.each_index {|i| self[i].each {|j, a_ij| b[j] ||= Array.new; b[j][i] = a_ij}}
    return b
  end
end

a = [{:a => 1, :b => 2, :c => 3}, {:a => 4, :b => 5, :c => 6}]
a.transpose #=> {:a=>[1, 4], :b=>[2, 5], :c=>[3, 6]}
+1
source

All Articles