Browse the Ruby on Rails Community.

You are here: Browse Railsplugins Sortable Column Headers

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:

  1. This will add all fields from the Reader table: add_to_sortable_columns(‘search’, Reader)
  1. This will add ‘books.title’: add_to_sortable_columns(‘search’, Book, ‘title’)
  1. This will add ‘books.author_id’ with an alias of ‘author’: add_to_sortable_columns(‘search’, Book, ‘author_id’, ‘author’)
  1. This will add ‘books.checked_out_at’: add_to_sortable_columns(‘search’, ‘books.checked_out_at’)
  1. This will add ‘some_field_as’
  2. (which you have specifically SELECTed AS in your SQL query): add_to_sortable_columns(‘search’, ‘book_check_in’)

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.
  1. DISCLAIMER: I haven’t tested this specific sample code. But you get the idea ;) ...

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