Rails 逐个而不是一页一页地对数组项进行分页
我有一群资产,暂且称他们为“实践者”吧。 我在日历界面的标题中显示这些从业者。 日历有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好问题!我想这是 WillPaginate 没有真正考虑到的一件事。我将在这里查看 WillPaginate 的代码,但我没有实际测试这个解决方案。如果您打算尝试一下,请告诉我它是否对您有用。
逻辑在
WillPaginate::Collection
中得到了很好的分离。您需要更改offset
和total_entries=
方法的行为。您可以通过子类化来做到这一点,但这意味着您不能再使用特殊的paginate
查找器,不幸的是。 (它有WillPaginate::Collection
硬编码。)您可能有类似以下内容,也许在您的
lib/
中:然后,您的示例代码将如下所示:
用法是我想有点复杂。所有额外的事情通常由特殊的查找器
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 theoffset
andtotal_entries=
methods. You can do this with subclassing, but that means you can no longer use the specialpaginate
finder, unfortunately. (It hasWillPaginate::Collection
hardcoded.)You could have something like the following, perhaps in your
lib/
:And then, your example code would look like:
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.我认为 LIMIT 适合你。我不知道使用分页,但您可以尝试以下
其中 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
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