Skip to content

Latest commit

 

History

History
110 lines (88 loc) · 2.67 KB

README.rdoc

File metadata and controls

110 lines (88 loc) · 2.67 KB

Description

table_for_collection is a simple gem used to render tables based on the given collection.

Install

Just add to your Gemfile

gem 'table_for_collection'

Simplest examle

<%= table_for @users do -%>
  <% columns :name, :email, :address %>
<% end %>

In this case you just put fields list to the :columns method. This ruby code will produce following HTML:

<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Email</th>
      <th>Address</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>John Smith</td>
      <td>john.smith@example.com</td>
      <td>100 Spear St., NY, USA</td>
    </tr>
  </tbody>
</table>

Simple examle

<%= table_for @users, :html => {
  :class => "simple-table",
  :id => "users",
  :width => "100%",
  :tr => {
    :class => "simple-row"
  }
} do -%>
  <% column :name, :html => { :th => { :width => "25%" }, :td => { :class => "user-name" }} %>
  <% column :email %>
  <% column :address, :title => "Home" %>
<% end %>

You can put :html hash to the options list (inside the :column method too). In this case table will be created with given html attributes. Also :title value in the :column method will be used as column’s title. It will produce HTML similar to:

<table class="simple-table" id="users" width="100%">
  <thead>
    <tr>
      <th width="25%">Name</th>
      <th>Email</th>
      <th>Home</th>
    </tr>
  </thead>
  <tbody>
    <tr class="simple-row">
      <td class="user-name">...</td>
      <td>...</td>
      <td>...</td>
    </tr>
  </tbody>
</table>

More complex example

<%= table_for @users do -%>
  <% column :login, :title => "User name" %>
  <% column :email do |email| %>
    <% mail_to email %>
  <% end %>
  <% column :title => "Full name" do |user| %>
    <% [user.first_name, user.last_name].join(" ") %>
  <% end %>
  <% column :company %>
  <% column :title => "Actions" do |user| %>
    <% [link_to("Show", user), link_to("Delete", user, :method => :delete)].join(" | ") %>
  <% end %>
<% end %>

Example with colorized rows

<%= table_for @users, :html => { :tr => { :class => "row" } }, :stripes => %w{even odd} do %>
  <% column :name %>
  <% column :email %>
  <% column :address %>
<% end %>

Also class given by :stripe option will be merged with options[:tr] so this code will produce following HTML:

<tr class="row even">...
<tr class="row odd">...
<tr class="row even">...
<tr class="row odd">...

Known issues

Unfortunately it works incorrect if there is no “-” before %> in this code:

<%= table_for @users do -%>

We hope it will be fixed soon.