【问题标题】:Adding dynamically rows to a table向表中动态添加行
【发布时间】:2013-05-24 13:48:07
【问题描述】:

基于 Railscasts 的 Prawn 剧集,我想添加这样的行:

def line_items
  move_down 20
  table line_item_rows do
    row(0).font_style = :bold
    columns(1..3).align = :right
    self.row_colors = ["DDDDDD", "FFFFFF"]
    self.header = true
  end
end

def line_item_rows
  [["Product", "Qty", "Unit Price", "Full Price"]] +
  @order.line_items.map do |item|
    [item.name, item.quantity, price(item.unit_price), price(item.full_price)]
  end
end

我想在“@order.line_items”行之后动态添加行,但我不知道该怎么做,然后我尝试了:

def line_item_rows
  [["Product", "Qty", "Unit Price", "Full Price"]] +
  @order.line_items.map do |item|
    [item.name, item.quantity, price(item.unit_price), price(item.full_price)]
  end

  @order.other_line_items.map do |item|
    [item.name, item.quantity, price(item.unit_price), price(item.full_price)]
  end
end

但它当然行不通。你知道怎么做吗?

【问题讨论】:

    标签: arrays ruby-on-rails-3 prawn


    【解决方案1】:

    解决方案很简单:

    data = data + 
      @order.other_line_items.map do |item|
        [item.name, item.quantity, price(item.unit_price), price(item.full_price)]
      end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-10
      • 1970-01-01
      • 1970-01-01
      • 2016-08-07
      • 2019-02-12
      • 1970-01-01
      • 2019-06-24
      相关资源
      最近更新 更多