Agung Prasetyo
Agung Prasetyo

Reputation: 4483

Customize will_paginate button

Is there any way to customize will_paginate control button to add more button like FIRST and LAST, not only previous and next buttons. Thank you.

Upvotes: 3

Views: 2255

Answers (2)

Mahesh
Mahesh

Reputation: 6436

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 %>

Upvotes: 1

Chris Barretto
Chris Barretto

Reputation: 9529

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

Upvotes: 3

Related Questions