views:

425

answers:

4

I'm using the will_paginate gem to display a collection in a paginated fashion. Now I'd like to have sorting functionality on the columns of the list in the view. I've added my own custom mechanism for establishing this, but it left me wondering if there isn't a standard way of doing this, leveraging the simplicity and elegance of will_paginate.

A: 

searchlogic plugs in nicely with will_paginate.

It doesn't sound like you're searching, but showing everything is just a special case for a search. You can use just the sorting features and ignore the rest if you don't wish to filter your data.

jdl
Not a direct answer to the question, but thanks for the usefull link.
Pascal Lindelauf
+1  A: 

Yes, There is an elegant way of doing it. Using ujs_sort_helper. You can find more details here

Teja Kantamneni
I like the fact that it allows for layout other than tables as well. Thanks.
Pascal Lindelauf
+1  A: 

On previous projects where I've implemented both sorting and paging, I've let will_paginate handle the paging while using overwrite_params to handle the sorting.

For example (assuming a table)

<tr>
  <th><%= link_to "ID", overwrite_params(:sort => "id", :page => 1) %></th>
  <th><%= link_to "Name", overwrite_params(:sort => "name", :page => 1) %></th>
</tr>
<tr>
  ... information ...
</tr>

overwrite_params does exactly what you'd think it does... it will give you back the current url with any existing params overwritten with the values specified, or any previously non-existing values inserted into the url.

Since will_paginate already preserves any url params, using overwrite_params in your column headers along with resetting the page by to 1 (which is optional), means that you get to continue to use the benefits of will_paginate without hacking it up to also handle your sorting.

jerhinesmith
+1  A: 

Java the Hutt's sortable_table
Go for broke. This is an AWESOME rails plugin. REALLY easy to set up.

Trevoke