繁体   English   中英

will_paginate与无尽的滚动| Rails4

[英]will_paginate with endless Scrolling | Rails4

这是解决方案

所以我正在使用will_paginate / Bootstrap Will Paginate with Endless Scrolling。

为了让分页工作:

1.)在我的控制器中,我更新了我的索引操作

@clips = Clip.order("created_at desc").page(params[:page]).per_page(20)

2.)编辑我的索引视图:

<%= will_paginate @clips%>

DONE

分页工作得很好。

To Add Endless scrolling我执行了与之前的Rails 3应用程序相同的步骤。

1.)编辑我的clips.js.coffee

jQuery ->
$('#clips-masonry').imagesLoaded ->
    $('#clips-masonry').masonry itemSelector: ".clips-masonry" # Thats my Masonry

if $('.pagination').length # Thats for the Endless Scrolling
    $(window).scroll ->
        url = $('.pagination .next_page a').attr('href')
        if url && $(window).scrollTop() > $(document).height() - $(window).height() - 50
            # What to do at the bottom of the page
            $('.pagination').text("Fetching more Clips...")
            $.getScript(url)
        $(window).scroll()

2.)使用以下命令创建index.js.erb:

$boxes = $('<%= j render(@clips) %>')

$('#clips-masonry').append( $boxes ).imagesLoaded( function(){
  $('#clips-masonry').masonry( 'reload');
});
<% if @clips.next_page %>
  $('.pagination').replaceWith('<%= j will_paginate(@clips) %>');
<% else %>
  $('.pagination').remove();
<% end %>

3.)将 format.js添加到我的Controller索引操作中

def index
    @clips = Clip.order("created_at desc").page(params[:page]).per_page(12)
    respond_to do |format|
        format.html
        format.js
    end
end

4.)我的_clip.html.erb包含div

 <div class="clip-box clips-masonry" data-no-turbolink>

好的,我得到了我的更新问题,每个遇到这个问题的人,这就是解决方案。

如果有人喜欢使用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>   

控制器:

  respond_to do |format|
    format.html
    format.js
  end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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