Offset problem with "keep_with_next (row)" option in a table. #153
-
Hello, I hope there are still people passing by here... I would like to use the "keep with next" option to make one (or more) row stay "attached" to another even when the other one moves to the next page. To do this, in the cell options, I implemented the "keep_with_next" option, which can be set to "true" or the number of rows to keep together. module PrawnTableCellTextExtension
def keep_with_next=(value)
value = nil if value === false
@_keep_with_next = value
end
def keep_with_next?
not(@_keep_with_next.nil?)
end
def keep_with_next
if @_keep_with_next === true
1
else
@_keep_with_next
end
end
end
module Prawn
class Table
class Cell
class Text
include PrawnTableCellTextExtension
end
end
end
end I wrapped the "start_new_page?" method for additional processing. module Prawn
class Table
alias :original_start_new_page? :start_new_page?
def start_new_page?(cell, offset, ref_bounds)
if cell.respond_to?(:keep_with_next?) # so not a dummy
if cell.keep_with_next?
#
# As much as rows we want
#
extra_h = 0
cell.keep_with_next.times do |i|
extra_h += cells.rows(cell.row + i).height
end
offset -= extra_h + 5 # <===== "+5"?
# doesn't work (for one specific row) without +5
end
end
original_start_new_page?(cell, offset, ref_bounds)
end
end Unfortunately, as you can see, I have to add the value 5 for it to work, without understanding why. I hope the code I provide below will help you understand. If not, I can, of course, provide more context. Thank you in advance for shedding light on this. Cheers |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Upon reviewing my code, I believe I have understood... Unfortunately, I cannot verify it yet. That is wrong: cell.keep_with_next.times do |i|
extra_h += cells.rows(cell.row + i).height
end This might be better: (cell.keep_with_next + 1).times do |i|
extra_h += cells.rows(cell.row + i).height
end |
Beta Was this translation helpful? Give feedback.
Thanks @pointlessone (and congratulations for your awesome work on Prawn/table by the way).
The final workable code is: