Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 971
Agung Prasetyo Avatar asked Jan 18 '26 20:01

Agung Prasetyo


1 Answers

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
like image 105
Chris Barretto Avatar answered Jan 21 '26 12:01

Chris Barretto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!