Rails 逐个而不是一页一页地对数组项进行分页

发布于 2024-09-04 19:42:46 字数 891 浏览 7 评论 0原文

我有一群资产,暂且称他们为“实践者”吧。 我在日历界面的标题中显示这些从业者。 日历有 7 列。 7 列 = 每个视图/页面 7 个从业者。 现在:

如果第一页显示从业者 1-7,当您进入下一页时,您将看到从业者 8-15,下一页 16-23,等等。

我想知道如何寻呼从业者,以便如果第一页向您显示从业者 1-7,下一页将向您显示从业者 2-8,然后是 3-9,等等。

我将非常感谢您提供的任何帮助。 这是我正在使用的 Rails 代码。

最好的问候,

哈里斯·诺维克

# get the default sort order
sort_order = RESOURCE_SORT_ORDER

# if we've been given asset ids, start our list with them
unless params[:asset_ids].blank?
  params[:asset_ids] = params[:asset_ids].values unless params[:asset_ids].is_a?(Array)
  sort_order = "#{params[:asset_ids].collect{|id| "service_provider_resources.id = #{id} DESC"}.join(",")}, #{sort_order}"
end

@asset_set = @provider.active_resources(:include => {:active_services => :latest_approved_version}).paginate(
  :per_page => RESOURCES_IN_DAY_VIEW,
  :page => params[:page],
  :order => sort_order
)

I have a group of assets, let's call them "practitioners".
I'm displaying these practitioners in the header of a calendar interface.
There are 7 columns to the calendar. 7 columns = 7 practitioners per view/page.
Right now:

if the first page shows you practitioners 1-7, when you go the next page you will see practitioners 8-15, next page 16-23, etc. etc.

i am wondering how to page the practitioners so that if the first page shows you practitioners 1-7, the next page will show you practitioners 2-8, then 3-9, etc. etc.

i would greatly appreciate any help you can offer.
here is the rails code i am working with.

best regards,

harris novick

# get the default sort order
sort_order = RESOURCE_SORT_ORDER

# if we've been given asset ids, start our list with them
unless params[:asset_ids].blank?
  params[:asset_ids] = params[:asset_ids].values unless params[:asset_ids].is_a?(Array)
  sort_order = "#{params[:asset_ids].collect{|id| "service_provider_resources.id = #{id} DESC"}.join(",")}, #{sort_order}"
end

@asset_set = @provider.active_resources(:include => {:active_services => :latest_approved_version}).paginate(
  :per_page => RESOURCES_IN_DAY_VIEW,
  :page => params[:page],
  :order => sort_order
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

老子叫无熙 2024-09-11 19:42:46

好问题!我想这是 WillPaginate 没有真正考虑到的一件事。我将在这里查看 WillPaginate 的代码,但我没有实际测试这个解决方案。如果您打算尝试一下,请告诉我它是否对您有用。

逻辑在 WillPaginate::Collection 中得到了很好的分离。您需要更改 offsettotal_entries= 方法的行为。您可以通过子类化来做到这一点,但这意味着您不能再使用特殊的 paginate 查找器,不幸的是。 (它有 WillPaginate::Collection 硬编码。)

您可能有类似以下内容,也许在您的 lib/ 中:

class SlidingWindowCollection < WillPaginate::Collection
  def offset
    current_page - 1
  end

  def total_entries=(number)
    @total_entries = number.to_i
    @total_pages   = [@total_entries - per_page, 1].max
  end
end

然后,您的示例代码将如下所示:

@asset_set_scope = @provider.active_resources(:include => {:active_services => :latest_approved_version})
@asset_set = SlidingWindowCollection.create(params[:page], RESOURCES_IN_DAY_VIEW, @asset_set_scope.count) do |pager|
  pager.replace(@asset_set_scope.all(:offset => pager.offset, :limit => pager.per_page, :order => sort_order))
end

用法是我想有点复杂。所有额外的事情通常由特殊的查找器paginate处理,例如计算条目总数并选择正确的条目。我想如果您打算经常做某事,您可以创建一个助手。

Good question! I guess this is one thing WillPaginate doesn't really account for. I'm going by looking at WillPaginate's code here, but I didn't actually test this solution. If you intend to try it, let me know if it worked for you.

The logic is well separated, in WillPaginate::Collection. You need to change the behavior of the offset and total_entries= methods. You can do this with subclassing, but that means you can no longer use the special paginate finder, unfortunately. (It has WillPaginate::Collection hardcoded.)

You could have something like the following, perhaps in your lib/:

class SlidingWindowCollection < WillPaginate::Collection
  def offset
    current_page - 1
  end

  def total_entries=(number)
    @total_entries = number.to_i
    @total_pages   = [@total_entries - per_page, 1].max
  end
end

And then, your example code would look like:

@asset_set_scope = @provider.active_resources(:include => {:active_services => :latest_approved_version})
@asset_set = SlidingWindowCollection.create(params[:page], RESOURCES_IN_DAY_VIEW, @asset_set_scope.count) do |pager|
  pager.replace(@asset_set_scope.all(:offset => pager.offset, :limit => pager.per_page, :order => sort_order))
end

Usage is a bit more complicated, I suppose. All the extra stuff is normally taken care of by the special finder paginate, such as figuring out the total number of entries and selecting the right entries. I suppose you could create a helper if it's something you intend to do often.

瑕疵 2024-09-11 19:42:46

我认为 LIMIT 适合你。我不知道使用分页,但您可以尝试以下

LIMIT params[:page], 7

其中 params[:page] 是页码,
因此,对于第 1 页,它将显示 1 中的 7 行,即 1-7
类似地,
对于第 2 页,它将显示 2 中的 7 行,即 2-8

I think LIMIT will work for you. I don't know using pagination but you can try following

LIMIT params[:page], 7

Where params[:page] is number of the page,
So for page 1 it will show 7 rows from 1 i.e. 1-7
Smilarly,
for page 2 it will show 7 rows from 2 i.e. 2-8

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文