使用Will_Paginate

时间:2018-06-17 17:34:52

标签: javascript ruby-on-rails infinite-scroll will-paginate

我正在尝试按照本教程进行操作:https://richonrails.com/articles/endless-scrolling要实施,但ReplaceWith似乎会被破坏:

Pagination appears despite ReplaceWith

这是我的show.js.erb

$('#suggestion-list').append('<%= escape_javascript render(@suggestions) %>');
$('.pagination').replaceWith('<%= escape_javascript will_paginate(@suggestions) %>');

这是我的application.js

$(document).ready(function() {
  if ($('.pagination').length) {
    $(window).scroll(function() {
      var url = $('.pagination .next_page').attr('href');
      if (url && $(window).scrollTop() > $(document).height() - $(window).height() - 50) {
        $('.pagination').text("Please Wait...");
        return $.getScript(url);
      }
    });
    return $(window).scroll();
  }
});

以下是show中的collections_controller操作:

  def show
    @suggestions = @collection.items.where(suggestion: true, chosen: false).paginate(page: params[:page])
    @keywords = @collection.items.where(chosen: true).joins(:search).order("score ASC").paginate(page: params[:page])
    respond_to do |format|
      format.html
      format.js
    end

这是我的show.html.erb

<div class="card h-100">
  <div class="card-body">
    <h4 class="card-title">Suggestions</h4>
    <p class="card-text"> 
      <ul class="list-group list-group-flush" id="suggestion-list">
        <% if @suggestions %>
            <% @suggestions.each do |s| %>
            <li class="list-group-item">
              <%= Hpricot.uxs s.search.term %>
              <%= link_to 'Add', collection_item_choose_path(@collection, s),{  method: :post, class: "btn btn-primary" } %><%= link_to 'X', collection_item_destroy_path(@collection, s),{  method: :post, class: "btn btn-primary" } %><br>
            </li>
          <% end %>
          <div id="infinite-scrolling">
            <%= will_paginate @suggestions %>
          </div>
        <% end %>
      </ul>
    </p>
  </div>
</div>

非常感谢任何帮助:)

0 个答案:

没有答案