自定义will_paginate按钮

3
有没有办法自定义 will_paginate 控制按钮,添加更多的按钮,例如首个和最后一个按钮,而不仅仅是上一页和下一页按钮。谢谢。
2个回答

3
第一页很简单:
link_to 'First Page', :action => :whatever, :page => 1

最后一页有点棘手,在你的模型类中添加以下内容:
class << self
  # number of records per page, from the will_paginate docs
  def per_page
    20
  end

  # takes a hash of finder conditions and returns a page number
  # returns 1 if nothing was found, as not to break pagination by passing page=0
  def last_page_number(conditions=nil)
    total = count :all, :conditions => conditions
    [((total - 1) / per_page) + 1, 1].max
  end
end 

现在你可以做:

link_to 'Last page', :action => :whatever, :page => Model.last_page_number

1

假设你正在对@users进行分页,那么将以下链接放入你的视图中

<%= link_to 'First Page', :action => 'index', :page => 1 %>
<%= will_paginate @users %>
<%= link_to 'Last Page', :action => 'index', :page => @users.page_count %>

如果 page_count 是一个返回页面数量的方法,那将是一个很好的解决方案。不幸的是,will_paginate 没有内置该方法。 - Chris Barretto
抱歉,但我在Rails 2.8中使用这种方法,它对我来说很有效。 - Mahesh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接