pengwynn / ujs_sort_helper

Unobtrusive sort helper for Rails

This URL has Read+Write access

Wynn Netherland (author)
Mon Apr 21 09:01:56 -0700 2008
ujs_sort_helper / README
dc93fdbf » Wynn Netherland 2008-04-21 Updated README 1 =UnobtrusiveSortHelper
2
3
4 This is a plugin version of Stuart Rackham's most excellent SortHelper helper. The link_to_remote ajax calls have been removed in favor of unobtrusive javascript using lowpro.js.
5
6 === Features
7
8 - Consecutive clicks toggle the column's sort order.
9 - Sort state is maintained by a session hash entry.
10 - Icon image identifies sort column and state.
11 - Typically used in conjunction with will_paginate plugin
12
13 === Usage
14
15 ====Controller:
16
17 def list
18 sort_init 'last_name'
19 sort_update
20 @items = Contact.find_all nil, sort_clause
21 end
22
23 ====Controller (using will_paginate)
24
25 def list
26 sort_init 'last_name'
27 sort_update
28
29 options = {:page => params[:page], :include => :addresses, :order => sort_clause
30
31 @contacts = Contact.paginate(options)
32 end
33
34 ====Layout (app/views/layouts/application.html.erb):
35
36 <%= stylesheet_link_tag "ujs_sort_helper"%>
37 styles for nifty sort arrow images
38
39 <%= javascript_include_tag :defaults%>
40 as long as prototype.js is included before lowpro and ujs_sort_helper
41
42 <%= javascript_include_tag "lowpro"%>
43 you SHOULD already have this ;-)
44
45 <%= javascript_include_tag "usj_sort_helper"%>
46 this is where ujs comes in
47 ====View (table header in index.rhtml):
48
49 <thead>
50 <tr>
51 <%= sort_header_tag('id', :title => 'Sort by contact ID') %>
52 <%= sort_header_tag('last_name', :caption => 'Name') %>
53 <%= sort_header_tag('phone') %>
54 <%= sort_header_tag('address', :width => 200) %>
55 </tr>
56 </thead>
57
58 - The ascending and descending sort icon images are sort_asc.png and sort_desc.png and reside in the application's images directory.
59 - Introduces instance variables: @sort_name, @sort_default.
60 - Introduces params :sort_key and :sort_order.
61
62
63
64 Copyright (c) 2005 Stuart Rackham, (c) 2008 Wynn Netherland released under the MIT license