【问题标题】:Hidden Counter Pagination Issue隐藏的反分页问题
【发布时间】:2012-10-18 22:17:44
【问题描述】:

我目前有一个按多个选择框排序的工作表。我最近按照本教程实现了无限/无限滚动:http://railscasts.com/episodes/114-endless-page-revised?view=asciicast

本教程在进行了几次更改后仍然有效,但我遇到了一个问题。我在第一列使用计数器,但问题是每次加载下一页时,计数器都会重置为 1,所以计数器就像 1..2..3.. 一直到 50,但是当我滚动到底部时,它会重置为 1。我目前在开始时显示 50 条记录,并且每次用户向下滚动到底部时再显示 50 条记录。我希望下一个记录是 51...52...53...等。然后下次滚动到底部时,从 101 开始,然后是 102...103...104...等等,例如排名列表。

为了获得部分内容,我遵循了本教程,该教程还告诉我如何制作计数器列: http://xianese.blogspot.com/2008/06/render-collection.html

在做了一些研究之后,我发现我使用的计数器几乎没有记录,并且有几个人报告了它的分页问题,​​这意味着它不仅仅是无限滚动的问题,而是分页问题(我正在使用 will_paginate)。在一个网站上,一个人说他们已经解决了分页问题,​​但没有说如何,现在帖子已经关闭,所以我知道有办法解决这个问题。

这是该网站的链接: http://www.pgrs.net/2007/07/20/render-partial-with-collection-has-hidden-counter/

任何可以帮助我的东西都将不胜感激。

这是我的代码:

articles.js.coffee

jQuery ->
   if $('.pagination').length
      $('#articles_table').scroll ->
              url = $('.pagination .next_page').attr('href')          
              if url &&  $('#articles_table')[0].scrollHeight -    
              $('#articles_table').scrollTop() < 700                  
                      $('.pagination').text('Fetching more users...')
                      $.getScript(url)
$('#articles_table').scroll()

index.html.erb

<table class="table table-striped table-bordered span8 table-condensed"     
 id="articles_table" >
<thead class="header">
  <tr>
      <th>ID</th>
      <th>Title</th>
      <th>Description</th>
      <th>Created_At</th>
  </tr>
</thead>
<tbody>
<%= render @articles %>

</tbody>

index.js.erb

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

_article.html.erb

<tr>
      <td> <%= article_counter +1 %> </td>
      <td> <%= article.Title %> </td>
      <td> <%= article.Description %> </td>
      <td> <%= article.Created_At %> </td>
</tr>

谢谢,

杰克

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 pagination auto-increment infinite-scroll


    【解决方案1】:

    经过大量研究,我发现这篇堆栈溢出帖子与我的不同,因为它使用了索引,但代码对我有用! persistent counter in Ruby/Rails for each

    这是我添加到 _article.html.erb 的代码:

    <% if params[:page].nil? || params[:page] == "0" || params[:page] == "1" %>
      <% x = 0 %>
    <% else %>
      <% page = params[:page].to_i - 1 %>
      <% x = page * 50 %>
    <% end %>
    

    我刚刚添加了一个 x,所以它是:article_counter + x + 1,其中 x 在上面的代码中定义。这是整个文件的样子:

    <% if params[:page].nil? || params[:page] == "0" || params[:page] == "1" %>
          <% x = 0 %>
        <% else %>
          <% page = params[:page].to_i - 1 %>
          <% x = page * 50 %>
        <% end %>
    <tr>
          <td> <%= article_counter +1 %> </td>
          <td> <%= article.Title %> </td>
          <td> <%= article.Description %> </td>
          <td> <%= article.Created_At %> </td>
    </tr>
    

    【讨论】:

    • 更短的版本:&lt;% page = params[:page] ? (params[:page].to_i - 1) * 50 : 0 %&gt;&lt;%= article_counter + 1 + page %&gt;
    猜你喜欢
    • 2019-09-26
    • 2019-09-02
    • 1970-01-01
    • 2014-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多