[Nitro] [PATCH] TableHelper sort implementation

Kashia Buch kashia at vfemail.net
Mon Jan 9 10:42:07 EST 2006


Hi,

I needed some kind of automatic sorting of table columns built into the TableHelper, so here's the patch. Please tell me of any quirks if you happen to use it :)

Sort Usage:
===========
#  <?r
#  users = User.all.map { |u| [u.name, u.first_name, u.last_name, u.email] }
#  headers = ['Username', 'First name', 'Last name', 'Email']
#  order_opts = { :right => true, # right align the order controls
#       :values => [nil, 'first_name', 'last_name'], # column names from DB
#       :asc_pic => "/images/asc.png",
#       :desc_pic => "/images/desc.png" }
#  ?>
#
#  <div class="custom-table-class">
#    #{table :values => users, :headers => header,
#     :order => order_opts, :alternating_rows => true }
#  </div>

If you don't provide pictures (:asc/desc_pic) to the order_opts it'll fall back to "^ v". Like you can see in the example, it doesn't put sort controls into the column where order_opts[:values] is nil/false.

Now, to use this feature in conjunction with for example a Pager put something along following into your controller:

# page = request.params[Pager.key]
# order_by = request.params[TableHelper.order_by_key] || 'oid'
# order_direction = request.params[TableHelper.order_direction_key] || "ASC"
# order = "#{order_by} #{order_direction}"
#
# entries, @pager = paginate(Container, :condition => "blbla",
#                :order => order, :per_page => 10, :page => page)

..

now, that I think about it.. can that stuff be put directly into the Pager?...


Alternating Rows
================
The additional :alternating_rows in the table controls produces html code like:

# <tr class="row_odd"></tr>
# <tr class="row_even"></tr>
#   ...

So you can use CSS to style the rows.

theader/tfooter/tbody Usage
===========================

When you like a little more control over the general layout of the table, you can use :footer and several <tbody> blocks like this:

#  <?r
#  admins = User.find(:condition => "rights = 'root'").map { |u| [u.name, u.first_name, u.last_name, u.email] }
#  users = User.find(:condition => "rights != 'root'").map { |u| [u.name, u.first_name, u.last_name, u.email] }
#  email_missing = User.count(:condition => "email IS NULL")
#  footer = ['', '', '', "#{email_missing} emails missing."]
#  ?>
#  <div class="custom-table-class">
#    #{table :values => [admins, users], :headers => header,
#     :order => order_opts, :alternating_rows => true,
#     :footer => footer }
#  </div>

produces code like:

# <table>
# <theader><tr> .. </tr></theader>
# <tfooter><tr> .. </tr></tfooter>
# <tbody> .. admin rows .. </tbody>
# <tbody> .. user rows ..</tbody>
# </table>

This can be used by CSS to style the header/footer/body of the table explicitly.

Hope someone can use it, every comment is helpful. (And I hope the code/usage is clear/clean enough... *sigh*)

Kash

-- 
Feel the love
http://pinkjuice.com/pics/ruby.png
-------------- next part --------------
A non-text attachment was scrubbed...
Name: table_helper_sort_implementation.tar.bz2
Type: application/bzip2
Size: 3838 bytes
Desc: not available
Url : http://rubyforge.org/pipermail/nitro-general/attachments/20060109/56aa7ff5/attachment.bin 


More information about the Nitro-general mailing list