You are here: Browse Railsplugins Sortable Column Headers
This plugin provides three handy methods which allow you to add sortability to your tabular data.
First, in your controller method you’ll want to specify what you want sorted.
def index
add_to_sortable_columns(sortable_name,*args)
...
end
The sortable_name is an arbitrary identifier which allows you to specify more than one sortable dataset for a given controller. Here are some examples of valid argument combinations:
Second, you’ll want to turn your column headers into links in your views:
<%= link_to 'ID', sort_param('search', Reader, 'name') %>
<%= link_to 'Title', sort_param('search', Book, 'title') %>
<%= link_to 'Author', sort_param('search', 'author') %>
<%= link_to 'Checkout', sort_param('search', 'books.checked_out_at') %>
<%= link_to 'Checkin', sort_param('search', 'book_check_in') %>
Third, going back to the controller, use the following method …
sortable_order(sortable_name, *args)
... to process the sort params in your page request and return a string containing all the sorted fields for use in an ORDER BY clause.
def index
add_to_sortable_columns('search', Reader)
add_to_sortable_columns('search', Book, 'title')
add_to_sortable_columns('search', Book, 'author_id', 'author')
add_to_sortable_columns('search', 'books.checked_out_at')
add_to_sortable_columns('search', 'book_check_in')
end
@books = Reader.find :all,
:include => [{ :books => :authors }],
:order => data_sort_order('search',Author,'id') # Will sort by 'authors.id' by default.
Fourth, BONUS METHOD! This plugin stores all sorts in the user session. You can reset the sorting history with the following method:
reset_sortable_columns
And that’s about it for now.
Cheers!
Colman
=============================================================================
SortableColumnHeaders a plugin for Ruby on Rails http://wush.net/svn/public/sortable_column_headers
Copyright© 2007 ELC Technologies Written by Colman Nady, ELC Technologies http://www.elctech.com
(Please read our MIT-style LICENSE in the attached file ‘MIT-LICENSE’.)
NOTE: This description has been extracted from the Plugin README and so the formatting may need updating to make browser friendly