Endless jquery page in rails 3 with will_paginate

Does anyone know how I can get around an endless page with jquery and will_paginate in rails 3? I tried so many ways, but it never worked for me.

+3
source share
3 answers

See Railscast # 114 Endless Page

You should be able to work with Rails 3 with minimal changes (if any).

+1
source

Your endless-page.js file will look something like this:

   var currentPage = 1;
   var autoloading = false;
   if( total_number_of_paginaion_pages > 1) {
       autoloading = true;
     }

function checkScroll() {
  if (autoloading && nearBottomOfPage()) {
    currentPage ++;
    autoloading = false;
    $.ajax( {
      url: window.location,
      data: 'page=' + currentPage,
      beforeSend: function() {
        $('.loading-info').show()
      },
      complete: function(){
        $('.loading-info').hide()
       },
      success: function(data){
        eval(data);
       }
     });
  } 
}

function nearBottomOfPage() {
  return scrollDistanceFromBottom() < 150;
}

function scrollDistanceFromBottom(argument) {
  return $(document).height() - ($(window).height() + $(window).scrollTop());
}

$(window).bind('scroll', function (){
         checkScroll();
});  

And in your file js.erb will look something like this.

$('.results-center').append('<%=escape_javascript(render :partial => '/search/search_result') %>');
if(! pagination_last_page) {
  autoloading = true;
}
+1
source

All Articles