prawnpdf / prawn-table

Provides support for tables in Prawn
Other
205 stars 98 forks source link

Alternating "row_colors" reset after page break #116

Open supermitch opened 5 years ago

supermitch commented 5 years ago

I noticed that when a table breaks across pages, the first alternating row_color "resets". Here is an example: (This is one table, broken by a page break. Notice how it should be green on white, but changes to red on white after the break)

If you also use cell.row % 2 to modify your text_color, they will go out of sync, because the row number is (correctly) maintained.

Simple reproduction here:

require 'prawn'
require 'prawn/table'

headers = [%w[Header1 Header2 Header3]]
data = headers + Array.new(30) { %w[Column1 Column2 Column3] }

Prawn::Document.generate('row-colors-test.pdf', page_layout: :landscape) do
  move_down 30
  font_size 9
  table(
    data,
    header: true,
    row_colors: %w[FFFFFF CCCCEE],
    cell_style: { border_width: 0.5 }
  ) do
    row(0).background_color = 'FCF6F2'
    cells.style do |c|
      if c.row > 0 && c.column == 2 # Skip headers & only modify col 3
        # c.row doesn't "reset" on new page, but row_color does
        c.text_color = (c.row % 2).zero? ? 'CC0000' : '00CC00'
      end
    end
  end
end

I believe that the row_color should not reset when a table breaks for a new page.