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

私はそれが最も美しいビューコードではないと思いますが(おそらくテーブルであるため)、解決策があります。

コントローラーで 3 つの配列を定義します( sortfor each 配列に注目してください)。すでに配列を持っている可能性があります。ただし、参考としてここにリストします。

@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 に答える