我想用我的数据创建一个如下所示的表,该数据存储在Ruby Arrays中:
______________________________________________________________
| BUILD | PLATFORM | CATEGORY |
|______________|________________|____________________________|
| | | IP |
| | 8k |____________________________|
| | | UMTS |
| |________________|____________________________|
| 10.0.1.50 | | IP |
| | |____________________________|
| | | UMTS |
| | 9k |____________________________|
| | | Stability |
|______________|________________|____________________________|
| | | IP |
| 10.0.1.51 | 8k |____________________________|
| | | UMTS |
|______________|________________|____________________________|
| | | IP |
| 11.0.1.50 | 9k |____________________________|
| | | UMTS |
|______________|________________|____________________________|
我的3个阵列中有以下数据:
Arr1
-------------------
row[0] : row[1]
-------------------
11.0.1.50 : 2
10.0.1.51 : 2
10.0.1.50 : 5
Arr2
---------------------------
row[0] : row[1] : row[2]
---------------------------
11.0.1.50 : 9k : 2
10.0.1.50 : 9k : 3
10.0.1.51 : 8k : 2
10.0.1.50 : 8k : 2
Arr3
-------------------------------
row[0] : row[1] : row[2]
-------------------------------
10.0.1.50 : 8k : IP
10.0.1.50 : 8k : UMTS
10.0.1.51 : 8k : IP
10.0.1.51 : 8k : UMTS
10.0.1.50 : 9k : IP
10.0.1.50 : 9k : Stability
10.0.1.50 : 9k : UMTS
11.0.1.50 : 9k : IP
11.0.1.50 : 9k : UMTS
数组中的所有这些数字基本上都给了我对该特定条目可能需要的行数的计数。
我在rails视图中编写了一些代码,但它似乎没有给我所需的输出:
<% @l1_reg.each_with_index do |row1, index1| %>
<table>
<tr>
<!-- Build -->
<td rowspan='<%= row1[1] %>'><strong><%= row1[0] %></strong></td>
<% @l2_reg.each_with_index do |row2, index2| %>
<% if ((row2[0].to_s == row1[0].to_s) %>
<!-- Platform -->
<td rowspan='<%= row2[1] %>'><strong><%= row2[0] %></strong</td>
<% @l3_reg.each_with_index do |row3, index3| %>
<% if ((row3[0].to_s == row2[0].to_s) && (row3[1].to_s == row2[1].to_s)) %>
<!-- Category -->
<td><%= row3[2] %></td>
<% end %>
<% end %>
<% end %>
<% end %>
</tr>
</table>
<% end %>
答案 0 :(得分:1)
我有一个解决方案,即使我认为它不是最美丽的视图代码(可能是因为它的表格)。
在控制器中定义三个数组(注意每个数组的sort
)。您可能已经有了数组。但是,我在这里列出它们作为参考:
@l1_reg = [['11.0.1.50', 2],
['10.0.1.51', 2],
['10.0.1.50', 5]].sort
@l2_reg = [['11.0.1.50', '9k', 2],
['10.0.1.50', '9k', 3],
['10.0.1.51', '8k', 2],
['10.0.1.50', '8k', 2]].sort
@l3_reg = [['10.0.1.50', '8k', 'IP'],
['10.0.1.50', '8k', 'UMTS'],
['10.0.1.51', '8k', 'IP'],
['10.0.1.51', '8k', 'UMTS'],
['10.0.1.50', '9k', 'IP'],
['10.0.1.50', '9k', 'Stability'],
['10.0.1.50', '9k', 'UMTS'],
['11.0.1.50', '9k', 'IP'],
['11.0.1.50', '9k', 'UMTS']].sort
然后,您可以在视图:
中执行以下操作<table>
<thead>
<tr>
<td>Build</td>
<td>Platform</td>
<td>Category</td>
</tr>
</thead>
<tbody>
<% @l1_reg.each do | build, l1_row_sum| %>
<% # platforms for current build
@l2_reg.select {|b| build == b[0]}.each_with_index do | l2, l2_index | %>
<% _, platform, l2_row_sum = l2 %>
<% # category for current build, plaform
@l3_reg.select {|b| build == b[0] and platform == b[1] }.each_with_index do | l3, l3_index | %>
<% category = l3.last %>
<tr>
<% if l2_index == 0 and l3_index == 0 %>
<td rowspan="<%= l1_row_sum %>"><%= build %></td>
<% end %>
<% if l3_index == 0 %>
<td rowspan="<%= l2_row_sum %>"><%= platform %></td>
<% end %>
<td><%= category %></td>
</tr>
<% end %>
<% end %>
<% end %>
</tbody>
</table>
答案 1 :(得分:1)
如果你像@tessi一样对数组进行排序,你可以更优雅地编写视图代码:
<% last_a = last_b = nil %>
<table>
<% @l3_reg.each do |a, b, c| %>
<tr>
<% if a != last_a %>
<% last_a, n = @l1_reg.shift %>
<td rowspan="<%= n %>"><%= last_a %></td>
<% end %>
<% if b != last_b %>
<% _, last_b, n = @l2_reg.shift %>
<td rowspan="<%= n %>"><%= last_b %></td>
<% end %>
<td><%= c %></td>
</tr>
<% end %>
</table>
我们循环第三个数组,因为它是行数。
对于第一列(a),我们看看我们放在那里的最后一个值(对于第一行,它是零)
如果电流与上次不同,我们从第一个数组中取出第一行(包含列的值及其行间距)并将值放在那里。
(shift正在从类似数组的反向弹出中删除第一个元素)
我们对第二列(b)做同样的事情
我们总是把列(c)的值放在每一行上。
作为奖励点,您可以轻松地使此版本独立于任意数量的列:)