6

我想在 HTML 表格中垂直排列三个数组。每个数组都将其数据从上到下填充在一列中。

例如,我有三个数组:

fruit = ['pear', 'apple', 'orange']
veges = ['corn', 'radish', 'lettuce']
meat = ['beef', 'chicken', 'pork']

我希望表格看起来像这样:

<table>
  <tr>
    <td>
      pear
    </td>
  </tr>
  <tr>
    <td>
      corn
    </td>
  </tr>
  <tr>
    <td>
      beef
    </td>
  </tr>

  <tr>
    <td>
      apple
    </td>
  </tr>
  <tr>
    <td>
      radish
    </td>
  </tr>
  <tr>
    <td>
      chicken
    </td>
  </tr>

  <tr>
    <td>
      orange
    </td>
  </tr>
  <tr>
    <td>
      lettuce
    </td>
  </tr>
  <tr>
    <td>
      pork
    </td>
  </tr>
</table>
4

2 回答 2

3

我可能会Array#transpose用来重新排列事物以匹配您的<table>外观:

rows = [ fruit, veges, meat ].transpose

现在rows看起来像:

[
  ["pear", "corn", "beef"],
  ["apple", "radish", "chicken"],
  ["orange", "lettuce", "pork"]
]

并生成您的表是一个简单的迭代问题rows

%table
  - rows.each do |row|
    %tr
      - row.each do |food|
        %td= food
于 2014-11-18T22:33:36.430 回答
1

看看这个网站:Generate vertical-ordered HTML table in Ruby

以下是相关代码(本示例中的实例变量仅用于清楚地识别控制列数和行数的因素):

<table>
  <tbody>
    <% 0.upto(@rows_per_column-1).each do |row| %>
      <tr>
        <% 0.upto(@columns-1).each do |column| %>
        <% index = row + (column * @rows_per_column) %>
        <td><%= index %></td>
        <% end %>
      </tr>
    <% end %>
  </tbody>
</table>
于 2012-05-31T23:54:23.507 回答