Table Cloth is a table view helper for Rails. It makes generating the HTML in Rails Views easy.
Table Cloth is a table view helper for Rails. It makes generating the HTML in Rails Views easy.
As much as I'd love for this project to be a solution for your projects tables needs, I simply haven't maintained it in too long.
Cheers,
Table Cloth gives you an easy to use DSL for creating and rendering tables in rails.
The primary goal of Table Cloth is to remove the complexity that usually comes with making tables with dynamic content. HTML Tables frequently can get out of hand when you start to add conditionals, removing columns, etc..
Follow me! @robertoross
Add this line to your application's Gemfile:
gem 'table_cloth'And then execute:
$ bundleTable Cloth can use defined tables in app/tables or you can build them on the fly.
Table models can be generated using rails generators.
$ rails g table UserIt will make this:
class UserTable Or if you want more customization:
You can create your own column by making a class that responds to .value(object, view)
class ImageColumn
You can add HTML attributes to all rows by passing in a hash.
class UserTable < DefaultTable
row_attributes class: "danger", name: "user-table-row"
column :name
endWhich would render something like
dangerous-john
totally-safe-bridgette
Or you can generate attributes for each row by passing in a block that is evaluated each time a row is created. This block must return a hash of attributes.
class UserTable < DefaultTable
row_attributes do |user|
css_class = user.is_dangerous? ? "danger" : ""
{class: css_class, name: "#{user.name}-row"}
end
column :name
endwhich would render something like
dangerous-john
totally-safe-bridgette
A lot of tables have an actions column to give you the full CRUD effect. They can be painful but Table Cloth incorporates a way to easily add them to your definition:
class UserTable < TableCloth::Base
column :name
actions do
action {|object| link_to 'View', object }
action(if: :admin?) {|object| link_to 'Delete', object, method: :delete }
end
def admin?
view.current_user.admin?
end
endCreate an initializer called table_cloth.rb
It should look like this:
TableCloth::Configuration.configure do |config|
config.table.class = 'table table-bordered'
config.thead.class = ''
config.tbody.class = ''
config.tr.class =''
config.th.class =''
config.td.class =''
endYou can also configure specific tables separately:
class UserTable < TableCloth::Base
column :name, :email
actions do
action {|object| link_to "Edit", edit_object_path(object) }
end
config.table.class = ''
config.thead.class = ''
config.th.class = ''
config.tbody.class = ''
config.tr.class = ''
config.td.class = ''
endYou can set any value on table element configurations. For example:
config.table.cellpadding = 1
config.td.valign = 'top'You also have the option to specify options on a specific column with the td_options key.
class UserTable < TableCloth::Base
column :name, td_options: { class: "awesome-column" }
endTable header (th) elements can be configured in a similar fashion with the th_options key.
Not good enough? Fine... you can do row / column specific config as well for a TD.
class UserTable < TableCloth::Base
column :name do |user|
[user.name, {class: "#{user.type}-user"}]
end
endThis would render something alow the lines of:
Robert Rossgit checkout -b my-new-feature)git commit -am 'Add some feature')git push origin my-new-feature)No open issues yet, or sync has not completed.