我有三个数组要在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>
最佳答案
我可能会使用Array#transpose
来重新排列,以匹配您的<table>
应该是什么样子:
rows = [ fruit, veges, meat ].transpose
现在
rows
将看起来像:[
["pear", "corn", "beef"],
["apple", "radish", "chicken"],
["orange", "lettuce", "pork"]
]
生成表是一个简单的迭代问题:
%table
- rows.each do |row|
%tr
- row.each do |food|
%td= food