开发者

De facto approach to sorting with will_paginate

开发者 https://www.devze.com 2022-12-17 11:59 出处:网络
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 vi开发者_开发知识库ew. I\'ve added my own

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 vi开发者_开发知识库ew. 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.


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


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消