百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
T

table_cloth

> 编程语言
开源

Table Cloth 是 Rails 的一个表格视图辅助工具,它使在 Rails 视图中生成 HTML 变得简单。

161 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

Table Cloth 是 Rails 的一个表格视图辅助工具,它使在 Rails 视图中生成 HTML 变得简单。

Unmaintained

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,

  • Bobby Tables

Table Cloth

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

Installation

Add this line to your application's Gemfile:

ruby
gem 'table_cloth'

And then execute:

$ bundle

Usage

Table 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 User

It will make this:

ruby
class UserTable 

Or if you want more customization:

erb

  
  
  
    
  

Columns

You can create your own column by making a class that responds to .value(object, view)

ruby
class ImageColumn 
  
  

Rows

You can add HTML attributes to all rows by passing in a hash.

ruby
class UserTable < DefaultTable
  row_attributes class: "danger", name: "user-table-row"

  column :name
end

Which would render something like

xml

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.

ruby
class UserTable < DefaultTable
  row_attributes do |user|
     css_class = user.is_dangerous? ? "danger" : ""
    {class: css_class, name: "#{user.name}-row"}
  end

  column :name
end

which would render something like

xml

dangerous-john

totally-safe-bridgette

Actions

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:

ruby
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
end

Configuration

Create an initializer called table_cloth.rb

It should look like this:

ruby
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 =''
end

You can also configure specific tables separately:

ruby
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    = ''
end

You can set any value on table element configurations. For example:

ruby
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.

ruby
class UserTable < TableCloth::Base
  column :name, td_options: { class: "awesome-column" }
end

Table 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.

ruby
class UserTable < TableCloth::Base
  column :name do |user|
    [user.name, {class: "#{user.type}-user"}]
  end
end

This would render something alow the lines of:

xml
Robert Ross

Thanks

  • TableCloth was built during my open source time at philosophie

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. CREATE A SPEC.
  4. Commit your changes (git commit -am 'Add some feature')
  5. Push to the branch (git push origin my-new-feature)
  6. Create new Pull Request

Issues· 8 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言