Fabian is proud to present you this little tip:

TableHelper tip

For faster table creation you can use the table handler.

Let's take a table with "nick", "first_name", "last_name" and "email". The result might look like:

NickFirst NameLast NameEmail
OgguFabianBuchf@b.com
manveruMichaelFellingerm@f.com
nick3Name3LName3n@3.com
nick4Name4LName44@n.com

a.k.a:

<table>
  <tr><th>Nick</th><th>First Name</th><th>Last Name</th><th>Email</th></tr>
  <tr><td>Oggu</td><td>Fabian</td><td>Buch</td><td>f@b.com</tr>
  <tr><td>manveru</td><td>Michael</td><td>Fellinger</td><td>m@f.com</tr>
  <tr><td>nick3</td><td>Name3</td><td>LName3</td><td>n@3.com</tr>
  <tr><td>nick4</td><td>Name4</td><td>LName4</td><td>4@n.com</tr>
</table>

Creating this out of an Og User object could be done as follows in a template:

<table>
  <tr><th>Nick</th><th>First Name</th><th>Last Name</th><th>Email</th></tr>
  <?r User.all.map { |u| ?>
    "<tr><td>#{u.nick}</td><td>#{u.first_name}</td><td>#{u.last_name}</td><td>#{u.email}</td></tr>"
  <?r } ?>
</table>

This is quite a lot html to write. So there's a Table Hanlder in Nitro which does the work for you.

First include the helper to your controller:

helper :table

Then define headers and data for your table in a controller action:

@headers = ['Username', 'First name', 'Last name', 'Email']
@users = User.all.map { |u| [u.nick, u.first_name, u.last_name, u.email] }

And display it somewhere in your template:

<div class="custom-table-class">
  #{table :values => @users, :headers => @header}
</div>

Which will look the same as above:

NickFirst NameLast NameEmail
OgguFabianBuchf@b.com
manveruMichaelFellingerm@f.com
nick3Name3LName3n@3.com
nick4Name4LName44@n.com

Of course the table helper has some more neat features, like ordering and alternating rows. For alternating rows it's sufficient to add :alternating_rows => true to your template:

<div class="custom-table-class">
  #{table :values => @users, :headers => @header, :alternating_rows => true}
</div>

and coloring it somehow via css:

.row_even {
  background-color: #99f;
}
.row_odd {
  background-color: #9f9;
}

It'll look like this:

NickFirst NameLast NameEmail
OgguFabianBuchf@b.com
manveruMichaelFellingerm@f.com
nick3Name3LName3n@3.com
nick4Name4LName44@n.com

There are some options to alter the way the table can be ordered. You can add them to the controller action:

@users = User.all.map { |u| [u.nick, 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" }

And just attach them to your table helper in the template:

<div class="custom-table-class">
  #{table :values => @users, :headers => @header, :alternating_rows => true, :order => @order_opts }
</div>