Problem building table

134 views
Skip to first unread message

Mark Gibson

unread,
Apr 25, 2013, 12:38:10 AM4/25/13
to prawn...@googlegroups.com
Hi Folks,

I am trying to build a single table from two data sources, line_items which has_many sub_items. Originally I had success doing this as a table within another table but there were problems when the sub table was larger than the page length. To avoid the sub table problem I'd like to build all the data into a single table. Here's what I have sor far:

  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, "", price(item.charge_ex_gst)] + 
      item.sub_items.map do |sub_item|
          [ "","#{sub_item.quantity} x #{sub_item.name}", price(sub_item.total_charge_ex_gst)] 
        end
    end +
    [["","Grand Total", price(@project.charge_ex_gst)]]
  end

When I run this page I get the "data must be a two dimensional array of cellable objects" error. here's what the generated data looks like:
[["Description", "Items", "Price ex GST"], ["LCD Monitor", "", "$9,134.00", ["", "1 x Sony 52 inch Monitor", "$1,464.00"], ["", "1 x Mounting bracket", "$390.00"], ["", "1 x Cables", "$130.00"], ["", "55 x 5 Colour Coded JBN Sound Ceiling Tiles", "$7,150.00"]], ["", "Grand Total", "$9,134.00"]]

I can see there is an extra "]" in the second last row but I'm not sure how to get rid of it. Any suggestions as to what I'm doing wrong here? I think I'm close.

Thanks, Mark Gibson

Brad Ediger

unread,
Apr 25, 2013, 5:31:15 PM4/25/13
to prawn-ruby
On Wed, Apr 24, 2013 at 11:38 PM, Mark Gibson <gibbom...@gmail.com> wrote:
> def line_item_rows
> [["Description", "Items" ,"Price ex GST"]] +
> @line_items.map do |item|
> [item.description, "", price(item.charge_ex_gst)] +
> item.sub_items.map do |sub_item|
> [ "","#{sub_item.quantity} x #{sub_item.name}",
> price(sub_item.total_charge_ex_gst)]
> end
> end +
> [["","Grand Total", price(@project.charge_ex_gst)]]
> end
>
> When I run this page I get the "data must be a two dimensional array of
> cellable objects" error

Are you trying to render one row for each sub-item, or are you trying
to show each sub-item in a new column in the same row as the item?
Currently, each iteration of the outer @line_items.map block comprises
one row of the table. If you're trying to create multiple rows via
that iteration, your best bet may be to try to convert that to an each
and build up the array manually:

rows = [["Description", "Items", "Price ex GST"]]
@line_items.each do |item|
rows << [... item information ...]
item.sub_items.each do |sub_item|
rows << [... subitem information ...]
end
end

Hope this helps!
-be

Mark Gibson

unread,
Apr 26, 2013, 9:38:23 PM4/26/13
to prawn...@googlegroups.com
Thanks Brad, that was exactly what I needed to fix it.

I originally tried to do it with nested tables but if a nested table became longer than a page there was all sorts of problems. Doing it this way only builds one table which handles page breaks properly.

Thanks so much for your help.

Cheers, Mark
Reply all
Reply to author
Forward
0 new messages