Ruby on Rails HTML-表格生成器

6
我正在寻找一款优秀的RoR表格生成器(或简单解决方案),可以为我在表格中提供记录的良好视图(未加样式但符合严格的XHTML标准)。
假设我有一个用户模型和一个地址模型: - 一个用户可以有多个地址 - 一个地址也链接为"主地址"
假设我在我的用户控制器中有以下内容:
def index
   @users = User.find(:all,:order => 'id ASC')
   @headers = ["id","First","Last","City","State"]
   @fields = [:id,:firstname,:lastname,:primary_address.city,:primary_address.state]
end

我不确定字段数组是否可行,但我认为这能传达出我的意思。有没有人知道一个好的gem、插件或技术可以做到这一点,这样我就不必在所有表视图中“重复自己”了?

6个回答

7

@ChrisH: 使用两个数组表示表格并不会提供更多的控制。我建议使用以下方式:table_helper

erb片段 -

collection_table(@posts, {}, :id => 'posts', :class => 'summary') do |header, body|
  header.column :title
  header.column :category
  header.column :author
  header.column :publish_date, 'Date< br \>Published'
  header.column :num_comments, '# Comments'
  header.column :num_trackbacks, '# Trackbacks'

  body.alternate = true
  body.build do |row, post, index|
    row.category       post.category.name
    row.author         post.author.name
    row.publish_date   time_ago_in_words(post.published_on)
    row.num_comments   post.comments.empty? ? '-' : post.comments.size
    row.num_trackbacks post.trackbacks.empty? ? '-' : post.trackbacks.size
  end
end

1
Rekin,链接现在对我有效,请再试一次。谢谢。 - Ninad

3
你可以使用助手来制作一个吗?
def table_generator(collection, header_names, fields)
  return false unless collection.any?
  content_tag(:table, :class => "generic-table") do
    content_tag(:thead) do
      content_tag(:tr) do
        header_names.each do |name|
          content_tag(:td, name)
        end
      end
    end
    content_tag(:tbody) do
      collection.each do |col|
        content_tag(:tr) do
          field_names.each do |name|
            content_tag(:td, col.send(name))
          end
        end
      end
    end
  end
end

请谨慎使用!未经测试。


这有点有帮助,但它并不完全奏效。不确定发生了什么事情。也许是我的输入不正确,但我无法生成表格。请注意,在两个 td 行后您还缺少结束标记。 - chrishomer
抱歉,是的,它应该有结束标签。你传入了什么参数?等我有空的时候会试一下,看看能否进行调试。 - Omar Qureshi

3
尝试使用Datagrid - 一款 Ruby 库,可帮助您构建和呈现类似表格的数据,具有以下功能:
  • 可自定义的筛选
  • 排序顺序
  • 本地化
  • 导出为 CSV

2

谢谢。看起来很有前途。仍然得到我两年前发布的问题的答案感觉很有趣! - chrishomer

1

我知道这样做不太好看,但是我在使用“content_tag”时遇到了很多问题,所以我决定不再纠结于它,而是保存在一个字符串中。虽然我更喜欢使用那个函数,但现在时间比优雅更重要。也许将来我会回过头去解决它,但现在这样做对功能没问题,而且还能强制执行更好的CSS实践。

def table_generator(collection, header_names, fields, class_name)
    return false unless collection.any?
    table_str = ""
table_str += "<table id=\"" + class_name + "\" class=\"" + class_name + "\">\n"
  table_str += "\t<thead>\n"
    table_str += "\t\t<tr>\n"
      header_names.each do |name|
        table_str += "\t\t\t<th>"
        table_str += name
        table_str += "</th>\n"
      end
    table_str += "\t\t</tr>\n"
  table_str += "\t</thead>\n"
  table_str += "\t<tbody>\n"
    collection.each do |col|
      table_str += "\t\t<tr>\n"
        fields.each do |name|
          table_str += "\t\t\t<td>\n"
            table_str += col[name].to_s
          table_str += "\t\t\t</td>\n"
        end
      table_str += "\t\t</tr>\n"
    end
  table_str += "\t</tbody>\n"
table_str += "</table>\n"
end

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接