16

这是解决方案

所以我使用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%>

完毕

分页工作得很好。

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 添加到我的控制器索引操作中

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>
4

2 回答 2

8

好的,我用我的更新问题解决了这个问题,每个偶然发现这个问题的人,这就是解决方案。

于 2013-09-01T03:26:39.993 回答
1

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

  }
});

索引.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
于 2018-06-25T14:15:33.293 回答