1

我目前有一个按几个选择框排序的工作表。我最近按照本教程实现了无限/无限滚动: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/

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

这是我的代码:

文章.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>

谢谢,

杰克

4

1 回答 1

1

经过大量研究后,我发现这篇堆栈溢出帖子与我的不同,因为它使用了索引,但代码对我有用! Ruby/Rails 中的持久计数器

这是我添加到 _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>
于 2012-11-28T23:22:11.487 回答