Created
November 13, 2012 15:28
-
-
Save adamrobbie/4066325 to your computer and use it in GitHub Desktop.
Rails Endless pagination
This file contains hidden or 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
jQuery -> | |
if $('.pagination').length | |
$(window).scroll -> | |
url = $('.pagination .next_page').attr('href') | |
if url && $(window).scrollTop() > $(document).height() - $(window).height() - 50 | |
$('.pagination').text("Fetching more examples...") | |
$.getScript(url) | |
$(window).scroll() |
This file contains hidden or 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
def index | |
@exampless = Examples.order("our_column").page(params[:page]).per_page(10) | |
end |
This file contains hidden or 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
gem 'will_paginate' |
This file contains hidden or 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
<div id="examples"> | |
<%= render @examples %> | |
</div> | |
<%= will_paginate @examples %> |
This file contains hidden or 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
// Our ajax triggered code that corresponds with the rails controller action method. | |
$('#example').append('<%= j render(@exampless) %>'); | |
<% if @exampless.next_page %> | |
$('.pagination').replaceWith('<%= j will_paginate(@examples) %>'); | |
<% else %> | |
$('.pagination').remove(); | |
<% end %> |
Thank you so much :)
For Rails 5 and TurboLinks 5, replace the first line of example.js.coffee as follows:
Replace jQuery ->
with $(document).on "turbolinks:load", ->
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks, man :)