INFINITE SCROLLING WITH WILL PAGINATE

When we want to use the Infinite Scrolling then sometimes we are facing problem with Infinite Scrolling with Pagination.

So I am giving some steps that will try to help for that : -

Firstly we have to add the gem

gem 'will_paginate', '~> 2.3.16'

Then in our controller

def show
     @users = User.all.paginate(:page => params[:page], :per_page => 1)
     respond_to do |format|
        format.js     
     end
end

After that in our view page

show.html.erb

   =render :partial => "user_list", :locals=>{:users=>@users}

_user_list.html.erb

   #product_specs
    %table
      %tbody
        %tr
          %td
 

               our content.....

    #infinite-scrolling      
         = will_paginate @users



    <script type="text/javascript">
       $(document).ready(function() {
             if ($('.pagination').length)

             {
                  $(window).scroll(function() 

                   {
                          var url = $(".pagination a[rel='next']").attr('href');       
                          if (url && $(window).scrollTop() > $(document).height() -              $(window).height() - 50) 

                          {         
                                 $('.pagination').show();
                                 $('.pagination').addClass('loader-icon').html('<img src="/images/ajax-loader-small.gif"/>');         
                                  $.ajax({
                                       url: url,
                                      type: 'get'
                                  });
                                 return;
                          }       
                  });
                  return $(window).scroll();
            }
       });

   </script>

show.js.erb:-

      $("#infinite-scrolling .pagination").replaceWith('<%= escape_javascript will_paginate(@users) %>');
     $('#product_specs > table > tbody:last-child').append("<%= escape_javascript(render(:partial => 'products/user_list')) %>");
     $('.pagination').hide();

  









Comments

Popular posts from this blog

psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

CARRIERWAVE - UPLOAD ANY MEDIA (AUDIO , VIDEO AND IMAGE ) FILE IN RAILS

SELECT COUNTRY WITH STATE AND IT'S CITIES RAILS

RAILS: UPLOADING PHOTOS VIA AMAZON S3 AND PAPERCLIP (UPLOADING FILES TO S3 IN RUBY WITH PAPERCLIP)

SEARCHING USING AJAX IN RAILS

UPLOAD ANY MEDIA FILE IN RAILS USING PAPERCLIP

UNINITIALIZED CONSTANT PAPERCLIP::STORAGE::S3::AWS

CURL COMMAND ON RUBY ON RAILS

CHECK PASSENGER RUBY PATH