我有一个可变数量的表,行数可变,我想让它们一个接一个地显示,但如果一个表不适合当前页面,则把它放在下一个然后继续。我把表放在一个交易中,所以我可以回滚然后打印它,如果高度适合在curent页面,但我如何获得表高度?
我目前有这个代码
pdf.transaction do
pdf.table @data,
:font_size => 12,
:border_style => :grid,
:horizontal_padding => 10,
:vertical_padding => 3,
:border_width => 2,
:position => :left,
:row_colors => ["FFFFFF","DDDDDD"]
pdf.move_down 20
#pdf.rollback
end
对此的任何帮助都会很棒。或者其他任何方式来做到这一点?
最诚挚的问候 瑞克
答案 0 :(得分:5)
@current_page = pdf.page_count
@roll = pdf.transaction do
pdf.move_down 20
pdf.table @data,
:font_size => 12,
:border_style => :grid,
:horizontal_padding => 10,
:vertical_padding => 3,
:border_width => 2,
:position => :left,
:row_colors => ["FFFFFF","DDDDDD"]
pdf.rollback if pdf.page_count > @current_page
end
if @roll == false
pdf.start_new_page
pdf.table @data,
:font_size => 12,
:border_style => :grid,
:horizontal_padding => 10,
:vertical_padding => 3,
:border_width => 2,
:position => :left,
:row_colors => ["FFFFFF","DDDDDD"]
end
我希望这对你有用: - )
答案 1 :(得分:5)
正如@ m-x所写的那样,出于安全原因(例如“group”),已禁用回滚,但仍未实现回滚。那么,这里我如何处理表的分页:
只需使用标题选项
pdf.table @data,
header: true, # You can use 'header: 2' if your header take two rows
font_size: 12,
border_style: :grid,
horizontal_padding: 10,
vertical_padding: 3,
border_width: 2,
position: :left,
row_colors: ["FFFFFF","DDDDDD"]
用你的例子:
t = pdf.make_table @data,
font_size: 12,
border_style: :grid,
horizontal_padding: 10,
vertical_padding: 3,
border_width: 2,
position: :left,
row_colors: ["FFFFFF","DDDDDD"]
if cursor - t.height < 0
start_new_page
end
t.draw
希望有所帮助
答案 2 :(得分:0)
我是一个对虾初学者,所以这可能不是最好的解决方案,但它应该有效。
如果考虑字体大小和垂直填充以及@data
中的记录数,可以获得表格高度,并且可以通过调用Prawn::Document.cursor
方法获取当前光标位置。
拥有这两个数字后,您应该能够检查表格是否适合此页面。如果没有,只需启动一个新的(通过调用Prawn::Document.start_new_page
方法)。
否则该表将自动中断并将在下一页继续。
答案 3 :(得分:0)
谢谢伊戈尔
我正在设置当前页面,然后在渲染新表之后和回滚之前设置new_page变量之前的事务中。然后我可以回滚并且如果新页面var&gt;当前页面var,如果它是启动新页面并打印表格。请参阅下面的代码。
问题是现在pdf.start_new_page说错误但是如果我只是把pdf.rollback行拿出来就行了。见下面的错误。
任何想法,任何一个或任何更简单的方法,必须有一个!!
感谢 瑞克
@current_page = pdf.page_count
pdf.transaction do
pdf.move_down 20
pdf.table @data,
:font_size => 12,
:border_style => :grid,
:horizontal_padding => 10,
:vertical_padding => 3,
:border_width => 2,
:position => :left,
:row_colors => ["FFFFFF","DDDDDD"]
@the_next_page = pdf.page_count
pdf.rollback
end
if @the_next_page > @current_page
pdf.start_new_page
pdf.table @data,
:font_size => 12,
:border_style => :grid,
:horizontal_padding => 10,
:vertical_padding => 3,
:border_width => 2,
:position => :left,
:row_colors => ["FFFFFF","DDDDDD"]
end
错误
> You have a nil object when you didn't expect it!
The error occurred while evaluating nil.identifier
Extracted source (around line #158):
155: end
RAILS_ROOT: C:/InstantRails/rails_apps/Macrotec-Application
Application Trace | Framework Trace | Full Trace
c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document.rb:302:in `go_to_page'
c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document/internals.rb:128:in `finalize_all_page_contents'
c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document/internals.rb:127:in `each'
c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document/internals.rb:127:in `finalize_all_page_contents'
c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document.rb:344:in `render'
C:/InstantRails/rails_apps/Macrotec-Application/app/views/quotations/show.pdf.prawn:158:in `_run_prawn_app47views47quotations47show46pdf46prawn'