1

我想使用存储在 Ruby 数组中的数据创建如下表:

______________________________________________________________
|    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 %>
4

2 回答 2

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>
于 2013-05-02T13:37:49.587 回答
1

如果您像@tessi answer中那样对数组进行排序,您可以更优雅地编写视图代码:

<% 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),我们查看放在那里的最后一个值(对于第一行,它是 nil)

如果当前与上次不同,我们从第一个数组中取出第一行(其中包含列 a 值及其行跨度)并将值放在那里。

(shift 是从数组中删除第一个元素,如反向弹出)

我们对第二列(b)做同样的事情

我们总是把 (c) 列的值放在每一行上。

作为奖励点,您可以轻松地使此版本独立于任意数量的列:)

于 2013-05-07T08:31:26.563 回答