【问题标题】:How do I format and style a sub table in Prawn?如何在 Prawn 中设置子表的格式和样式?
【发布时间】:2013-04-11 01:14:43
【问题描述】:

我是使用 Prawn 生成 PDF 的新手,所以这可能是一个简单的问题,但它让我发疯了!

我有一个带有嵌套子表的表。我已经能够轻松地设置主表的样式和格式,但我似乎不能对嵌套的子表做同样的事情。我真正需要做的就是设置列宽并删除边框,但我似乎无法弄清楚。

这是我目前的代码:

  def line_items
    data = line_item_rows
    table(data)  do
       row(0).font_style = :bold
       columns(0).width = 160
       columns(1).width = 300
       columns(2).align = :right
       columns(2).valign = :bottom
       row(0).columns(2).valign = :top
       row(0).columns(2).align = :left
       self.header = true
    end   
  end

  def line_item_rows
    [["Description", "Items" ,"Price ex GST"]] +
    @line_items.map do |item|
      [item.description, sub_item_rows(item), price(item.charge_ex_gst)] 
    end +
    [["","Total", price(@project.charge_ex_gst)]]
  end

  def sub_item_rows(item)
   item.sub_items.map do |sub_item|
      ["#{sub_item.quantity} x  #{sub_item.name} #{price(sub_item.total_charge_ex_gst)}"] 
    end
  end

关于如何将样式应用于子表的任何建议? 提前感谢您的帮助。

干杯,马克

【问题讨论】:

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


    【解决方案1】:

    好的,这就是它的修复方法。我需要使用“make_table”并在那里应用格式:

      def line_items
        move_down 15
        data = line_item_rows
    
            table(data) do
               row(0).font_style = :bold
               columns(0).width = 160
               columns(1).width = 300
               columns(2).align = :right
               columns(2).valign = :bottom
               row(0).columns(2).valign = :top
               row(0).columns(2).align = :left
               self.header = true
            end     
      end
    
      def line_item_rows
        [["Description", "Items" ,"Price ex GST"]] +
        @line_items.map do |item|
          [item.description, 
            sub_items(item), 
            price(item.charge_ex_gst)] 
        end +
        [["","Grand Total", price(@project.charge_ex_gst)]]
      end
    
      def sub_items(item)
        sub_data = sub_item_rows(item)
        make_table(sub_data) do
           columns(0).width = 200
           columns(1).width = 100
           columns(1).align = :right
           #columns(0).borders = []
        end   
      end
    
      def sub_item_rows(item)
       item.sub_items.map do |sub_item|
          ["#{sub_item.quantity} x #{sub_item.name}", price(sub_item.total_charge_ex_gst)] 
        end  +
          [["","Total"]]
      end
    

    【讨论】:

    • 哇,谢谢先生分享,真的是我需要的。真的,在互联网上,大虾的轨道并不多,说真的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-25
    相关资源
    最近更新 更多