Created
September 5, 2008 21:16
-
-
Save ubermajestix/9029 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# written by Tyler Montgomery tyler[dot]a[dot]montgomery[at]gmail | |
#--- | |
# tyler_paginate accepts and will_paginate collection and the standard link_to_remote options to build ajaxy | |
# pagination links | |
#--- | |
# in your view: | |
# <%= tyler_paginate @collection, :url=>{:action=>:pagination_action, :id=>@some_object_id}, :update=>"some_div" %> | |
# generates something like: < previous 11-20 of 457 next > | |
#--- | |
# in your controller | |
# def pagination_action | |
# @collection = SomeModel.paginate(...) | |
# render :partial => "/some_partial_to_render" | |
# end | |
def tyler_paginate(collection, opts={} ) | |
#next / prev / pages | |
page_count = WillPaginate::ViewHelpers.total_pages_for_collection(collection) | |
pagination_method = collection.first.class.to_s.downcase + "_pagination" | |
start = ((collection.current_page-1) * collection.per_page ) + 1 | |
last = (start + collection.per_page - 1) > collection.total_entries ? collection.total_entries : (start + collection.per_page - 1) | |
header = "#{start} - #{last} of #{collection.total_entries}" | |
if collection.total_entries > collection.per_page | |
if page_count > 1 | |
if collection.offset != 0 | |
opts[:url][:page] = collection.previous_page | |
previous_link = "<a style=\"cursor:pointer; font-size:12px\" onclick=\"#{remote_function opts}\"> < previous </a>" | |
end | |
unless collection.current_page + 1 > page_count | |
opts[:url][:page] = collection.next_page | |
next_link = "<a style=\"cursor:pointer; font-size:12px\" onclick=\"#{remote_function opts}\"> next > </a>" | |
end | |
end | |
end | |
"<span style=\"font-size:12px\">#{previous_link.to_s||="" } #{header.to_s} #{next_link.to_s||=""} </span>" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment