简体   繁体   中英

jquery endless page in rails 3 with will_paginate

Does any one know how i can go about endless page with jquery and will_paginate in rails 3? I have tried so many ways but it has never worked for me.

the best option for implementing endless pagination will be using will_paginate / kaminari gem with jquery.

there is a good blog on how to implement endless page and hopefully will answer your question.have a look

http://www.idyllic-software.com/blog/endless-page-using-jquery-and-will_paginate/

See Railscast #114 Endless Page

You should be able to make it work with Rails 3 with minimal modifications (if any).

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 js.erb file will look something like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM