Matt,
No, you are making sense. I think you and I got mixed up backwards.
If you are KEEPING sort order but CHANGING pages, then don't change
the sort.
If you are KEEPING pages but CHANGING sort order, then I would argue,
force a page change to page 1.
So, in your example, if you sort by hostname ASC then go to page 2, it
should maintain the hostname sort. But if you are on page 2 and then
change your sort to IP address, it should jump you to page 1 (although
as I stated in my previous post, you could stay on page 2 in this
instance).
It's not a big deal programmatically because you take your collection
of objects first (i.e. via find() ), then you sort, then you paginate.
The only thing that could mess this up is if your dataset changes
between pages. If that is a concern, then you'd want to think through
the best way to handle that, which could be one of multiple options.
So, to reiterate, here's how I handle it. Here is my index method for
widgets:
[code]
def index
@widgets = paginate_and_sort Widgets.find(:all) # or whatever means
to get an array of widget objects
end
[/code]
Then, I have this defined on application.rb (cause I use it all over):
[code]
def paginate_and_sort(collection)
per_page = (session[:per_page] || 5).to_i
page = (params[:page] ||= 1).to_i
page = 1 if (page - 1) * per_page > collection.size or page < 1
offset = (page - 1) * per_page
@sorted_on = params[:sort] unless params[:sort_direction] == "reset"
@sorted_dir = params[:sort_direction]
if (["ASC", "DESC"].include?(@sorted_dir))
collection = if @sorted_dir == "DESC"
collection.sort {|a,b| b.send(params[:sort]) <=>
a.send(params[:sort])}
else
collection.sort {|a,b| a.send(params[:sort]) <=>
b.send(params[:sort])}
end
else
collection = collection.sort {|a,b| b.updated_at <=>
a.updated_at }
end
WillPaginate::Collection.new(page, per_page,
collection.length).concat(collection[offset..(offset + per_page - 1)])
end
[/code]
I then set the session[:per_page] elsewhere, i.e. when the user
changes that in a different action, I set it as a session variable.
So note that though not the cleanest code, it works and works well. I
build the values needed for will_paginate then I sort the collection
either by the sort & direction or by updated_at (that's my default,
yours may differ). Then pass that into will_paginate which will return
a pseudo-array of the paginated objects.
HTH!
-Danimal