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();

  









Post a Comment

Popular posts from this blog

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

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

SELECT COUNTRY WITH STATE AND IT'S CITIES RAILS

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

HOW TO CALL RAILS API FROM SIMPLE HTML PAGE.

Building native extensions. This could take a while... ERROR: Error installing mysql: ERROR: Failed to build gem native extension.

CREATE API FOR USER SIGN UP, LOGIN AND CREATE PUBLISHER WITH CURRENT USER

UPLOAD ANY MEDIA FILE IN RAILS USING PAPERCLIP