will_paginate with endless Scrolling | Rails4

后端 未结 2 1939
梦如初夏
梦如初夏 2020-12-24 08:34

THIS IS THE SOLUTION

So i\'m using will_paginate / Bootstrap Will Paginate with Endless Scrolling.

To get the Pagination working:

相关标签:
2条回答
  • 2020-12-24 08:52

    Ok, i got it working with my Updated Question, everyone who stumbles upon this problem, This is the solution.

    0 讨论(0)
  • 2020-12-24 09:07

    Incase someone prefers to use JS/JQUERY:

    $(window).scroll(function() {
    
      var url;
    
      // Checks if products are currently being loaded
      if (window.pagination_loading) {
        return;
      }
    
      // Grabs the URL from the "next page" button 
      url = $('.pagination .next_page').attr('href')
    
      // Chckes if you're n height from the bottom
      if (url && $(window).scrollTop() > $(document).height() - $(window).height() - 50) {
    
        // Variable to know that you are currently loading products
        window.pagination_loading = true;
    
        // Text while loading
        $('.pagination').text('');
    
        // Run the script
        return $.getScript(url).always(function() {
          return window.pagination_loading = false;
        });
    
      }
    });
    

    Index.js.erb:

    $products = $('<%= j render(@products) %>')
    
    $('#productsContainer').append( $products );
    
    <% if @products.next_page %>
      $('.pagination').replaceWith('<%= j will_paginate(@products) %>');
    <% else %>
      $('.pagination').remove();
    <% end %>
    

    index.html.erb

    <div class="container">
        <%= will_paginate @products %>
    </div>   
    

    Controller:

      respond_to do |format|
        format.html
        format.js
      end
    
    0 讨论(0)
提交回复
热议问题