Skip to content
This repository

A view simple and useful table builder for a collection of objects

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 app
Octocat-spinner-32 lib
Octocat-spinner-32 rails add these new files May 03, 2011
Octocat-spinner-32 spec
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .rspec add these new files May 03, 2011
Octocat-spinner-32 CHANGELOG.rdoc
Octocat-spinner-32 Gemfile
Octocat-spinner-32 README.rdoc
Octocat-spinner-32 Rakefile
Octocat-spinner-32 VERSION
Octocat-spinner-32 table-for.gemspec
README.rdoc

table-for

Table-for is a table builder for a collection of domain objects. It very easily allows the user to specify the columns to render and to override how the table, the header columns, the rows, and the columns are rendered.

Installation

In Rails 3, add this to your Gemfile.

gem "table-for"

In this example, I'm going to assume the following setup: From the console, I have generated a user model with email, first_name, and last_name fields, and generated a scaffold on that model

rails g scaffold user email:string first_name:string last_name:string --no-stylesheets

In seeds.rb:

User.create! :email => "andrew.hunter@livingsocial.com", :first_name => "Andrew", :last_name => "Hunter"
User.create! :email => "todd.fisher@livingsocial.com", :first_name => "Todd", :last_name => "Fisher"
User.create! :email => "jon.phillips@livingsocial.com", :first_name => "Jon", :last_name => "Phillips"

In users_controller.rb (index action)

@users = User.all

Sample Usage

<%= table_for @users, :table_html => {:style => "border: 1px solid black"},
                      :sortable => true,
                      :row_html => {:class => lambda { cycle('even', 'odd')},
                                    :id => lambda {|user| "user-#{user.id}"}} do |table| %>
  <%= table.column :edit %>
  <%= table.column :show %>
  <%= table.column :email, :label => "Email Address" %>
  <%= table.column :label => "Full Name", :sortable => false, :header_html => {:style => "color:orange"} do |user| %>
    <%= "#{user.first_name} #{user.last_name}" %>
  <% end %>
  <%= table.column :delete %>
<% end %>

Specifying the columns to use

<%= table_for @users do |table| %>
  <% table.column :email %>
  <% table.column :first_name %>
  <% table.column :last_name %>
<% end %>

Here, each column will send its respective column name to the user object: user.send(:email), user.send(:first_name), user.send(:last_name), to determine what data to output in each column

Specifying the label to use for a column

<%= table_for @users do |table| %>
  <% table.column :email, :label => "Email Address" %>
<% end %>

Overridding the default way or rendering a column

<%= table_for @users do |table| %>
  <% table.column :name do |user| %>
    <%= "#{user.first_name} #{user.last_name}" %>
  <% end %>
<% end %>

Here, since the name of the column is not used anymore to determine what data to output, this code could be changed to:

<%= table_for @users do |table| %>
  <% table.column :full_name do |user| %>
    <%= "#{user.first_name} #{user.last_name}" %>
  <% end %>
<% end %>

Or the block name could have been removed altogether and replaced with a label as follows:

<%= table_for @users do |table| %>
  <% table.column :label => "Full Name" do |user| %>
    <%= "#{user.first_name} #{user.last_name}" %>
  <% end %>
<% end %>
Something went wrong with that request. Please try again.