简体   繁体   English

如何使用Rails will_paginate gem划分一张大表以便在单个页面上打印

[英]How to Use Rails will_paginate gem to Divide a Large Table for Print on a Single Page

I am making some printable tables for a client with a Ruby on Rails 3.1 app and need to repeat table headers on each page. 我正在使用Ruby on Rails 3.1应用程序为客户端制作一些可打印的表格,并且需要在每个页面上重复表格标题。 Unfortunately, at the moment, WebKit browsers do not support a CSS-based solution. 不幸的是,目前, WebKit浏览器不支持基于CSS的解决方案。

To solve this issue, I thought I would use the will_paginate gem. 为了解决这个问题,我想我会使用will_paginate gem。

Controller 控制者

def
  @books = current_library.books.order('books.title ASC')
end

Current View Code 当前查看代码

<% @books.each do |b| %>
<table>
  <thead><th><%= b.title %></th></thead>
  <tbody>  
  <% b.chapters.each do |chap| %
      <td><%= chap.number %> ... <%= chap.name %></td>
  <% end %>
  </tbody>
</table>
<% end %>

How do I setup the pages and step through each one? 如何设置页面并逐步浏览每一页? In other words, how do I get all the pages of the pagination on one view page? 换句话说,如何在一个视图页面上获得所有分页页面?

Alternatively, is there a better approach I should pursue? 另外,我应该追求更好的方法吗?

You might be better off using Enumerable#each_slice here. 您最好在此处使用Enumerable#each_slice It allows you to split a large enumerable object into a series of smaller slices, and then iterate on those slices. 它允许您将大型可枚举的对象拆分为一系列较小的切片,然后在这些切片上进行迭代。 It's quite nice for this sort of thing, doesn't require any extra math in your loops, and doesn't require a gem. 这种事情非常好,不需要在循环中进行任何额外的数学运算,也不需要gem。

Here's an example for a collection with 10 items on a page: 这是一个在页面上包含10个项目的集合的示例:

<% @books.each_slice(10) do |slice| %>
  <h1>Header Information</h1>
  <h2>Awesome</h2>

  <% slice.each do |book| %>
    <table>
      <thead><tr><th><%= book.title %></th></tr></thead>
      <tbody>  
      <% book.chapters.each do |chap| %
          <tr><td><%= chap.number %> ... <%= chap.name %></td></tr>
      <% end %>
      </tbody>
    </table>
  <% end %>

  <p>Some footer information</p>
<% end %>

This approach will only work if you assume that each book record takes about the same amount of space (so you don't end up with oversized or undersized pages), but that would be a problem with will_paginate as well. 这种方法仅在假设每个书记录占用大约相同的空间量(因此不会导致页面过大或过小)的情况下起作用,但是will_paginate也将是一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM