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

On Sep 9, 10:39 am, Matt Harrison <[EMAIL PROTECTED]>
wrote:
> Danimal wrote:
> > Matt,
>
> > I find that the best is to just reset to the beginning with any change
> > of sort or pagination. It's too confusing not to. For example, if you
> > are showing 5 records per page in a 50 record set and you are on page
> > 4, then you suddenly switch to 25 records per page, what should it do?
> > Throw an error (there is no page 4 anymore)? Or have you on page 2? Or
> > back to page 1?
>
> > And with sorting, if I'm on page 4 of 7 pages sorted by last name,
> > then suddenly switch to sorting by first name, staying on page 4... at
> > least to me... is more confusing than just reverting back to page one.
> > Of course, you could stay on page 4 with the new sort. So sorting
> > seems to be more of a design choice. But pagination changes: go back
> > to page 1.
>
> > -Danimal
>
> Hi, thanks for the reply.
>
> I agree that there are some cases where it should reset the page or the
> sort. OTOH picture this:
>
> I'm on page 1 and I sort by hostname ASC, it gives me the hostnames
> starting at A etc. Then I want to move through this sorted list so I go
> to page 2, and my sort has been reset because I can't work out how to
> modify the links generated by paginating_find.
>
> I'm not fiddling with the page size yet so that isn't an issue, and it
> probably should go back to page 1 if I change the sort order, however I
> really would think that keeping the sort order when changing pages is
> kinda important.
>
> I might be going mad with this so let me know if i'm babbling :P
>
> Thanks
>
> Matt
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to