Rails 3: 矩阵网格视图

4
我需要在表格中显示多维数组数据,如下所示:
对于 sla.expected_billing,有一个包含12个周期的网格在左侧(行)
在顶部(列)有7个分类号
适用字段中有金额,但可能有空值
例如,
第1期:分类号1 = 400.00、分类号2 = null、分类号3 = 250.55、分类号4 = 500等等。
第2期:分类号1 = null、分类号2 = null等等。
 class ExpectedBilling < ActiveRecord::Base
   belongs_to :sla
   belongs_to :period
   belongs_to :ledger  
 end

 class Period < ActiveRecord::Base
   has_many :expected_billings  
 end

 class Ledger < ActiveRecord::Base
   has_many :expected_billings  
 end

 class Sla < ActiveRecord::Base
   has_many :expected_billings  
 end

我在考虑使用Matrix库和/或Class.transpose来解决问题,但我不确定。当任何给定数组中可能有空值时,我担心它无法正确地填充网格。有人能指导我正确的方向吗?

我遇到了类似的问题。你找到解决方案了吗? - Doug Johnston
1个回答

0

我会稍微不同地对其进行建模:

 class Sla < ActiveRecord::Base
   has_one :expected_billing
 end

 class ExpectedBilling < ActiveRecord::Base
   has_many :periods
   belongs_to :sla
 end

 class Period < ActiveRecord::Base
   has_many :ledgers
   belongs_to :expected_billing
 end

 class Ledger < ActiveRecord::Base
   belongs_to :period
 end

在视图中打印您的表格:

 <table>
   <!-- insert header row here -->
   <% sla.expected_billing.periods.each do |period| %>
     <tr>
       <!-- insert header column here -->
       <% period.ledgers.each do |ledger| %>
         <td><%= ledger.value %></td>
       <% end %>
     </tr>
   <% end %>
 </table>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接