开发者

Customize will_paginate button

Is there any way to customize will_paginate control button to add more button lik开发者_开发百科e FIRST and LAST, not only previous and next buttons. Thank you.


The first page is pretty easy:

link_to 'First Page', :action => :whatever, :page => 1

The last page a little tricky, in your model class add:

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 

now you can do:

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


Suppose you are paginating @users then put these links in your view

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜