问题描述:

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

)

网友答案:

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.

网友答案:

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

相关阅读:
Top