I am looking for a good RoR table generator (or an easy solution) that can give me a decent view of my records in a table (unstylized but proper strict XHTML).
我正在寻找一个好的RoR表生成器(或一个简单的解决方案),它可以在一个表中给我一个体面的记录(unstylized但适当严格的XHTML)。
Let's say I have a User model and an Address model: - A User can have many Addresses - One address is also linked as the "primary_address"
假设我有一个用户模型和一个地址模型: - 用户可以有多个地址 - 一个地址也链接为“primary_address”
Let's say I have the following in my User controller
假设我的用户控制器中有以下内容
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
I don't know if the array of fields would work but I think it gets the point across. Does anyone know a good gem, plugin or technique for this so that I don't have to "repeat myself" on all my table views?
我不知道字段数组是否可行,但我认为它可以解决问题。有没有人知道一个好的宝石,插件或技术,所以我不必在我的所有表视图上“重复自己”?
6 个解决方案
#1
6
@ChrisH: Representing table using two arrays won't give more control. I would suggest the following: table_helper
@ChrisH:使用两个数组表示表将不会提供更多控制。我建议如下:table_helper
erb snippet -
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
#2
3
you could make one using a helper?
你可以用帮手制作一个?
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
Use with caution! Untested.
谨慎使用!未经测试。
#3
2
This is the one I ended up using in the same situation:
这是我在相同情况下最终使用的那个:
https://github.com/lunich/table_for
https://github.com/lunich/table_for
#4
2
Try Datagrid - ruby library that helps you to build and represent table-like data with:
尝试使用Datagrid - ruby库,它可以帮助您构建和表示类似于表的数据:
- Customizable filtering
- 可定制的过滤
- Columns
- 列
- Sort order
- 排序
- Localization
- 本土化
- Export to CSV
- 导出为CSV
#5
1
I know this isn't pretty but I was having so many problems with the "content_tag" I decided it wasn't worth my time and just saved in a string. I would much rather use that function but time is more valuable than elegance right now. Maybe I'll go back and figure it out in the future, but for now, this is functional and forces better CSS practices anyway.
我知道这不是很漂亮但是我在“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
#6
1
I recently started a project on GitHub, give it a try: https://github.com/cthulhu666/easy_table
我最近在GitHub上开始了一个项目,试一试:https://github.com/cthulhu666/easy_table
#1
6
@ChrisH: Representing table using two arrays won't give more control. I would suggest the following: table_helper
@ChrisH:使用两个数组表示表将不会提供更多控制。我建议如下:table_helper
erb snippet -
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
#2
3
you could make one using a helper?
你可以用帮手制作一个?
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
Use with caution! Untested.
谨慎使用!未经测试。
#3
2
This is the one I ended up using in the same situation:
这是我在相同情况下最终使用的那个:
https://github.com/lunich/table_for
https://github.com/lunich/table_for
#4
2
Try Datagrid - ruby library that helps you to build and represent table-like data with:
尝试使用Datagrid - ruby库,它可以帮助您构建和表示类似于表的数据:
- Customizable filtering
- 可定制的过滤
- Columns
- 列
- Sort order
- 排序
- Localization
- 本土化
- Export to CSV
- 导出为CSV
#5
1
I know this isn't pretty but I was having so many problems with the "content_tag" I decided it wasn't worth my time and just saved in a string. I would much rather use that function but time is more valuable than elegance right now. Maybe I'll go back and figure it out in the future, but for now, this is functional and forces better CSS practices anyway.
我知道这不是很漂亮但是我在“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
#6
1
I recently started a project on GitHub, give it a try: https://github.com/cthulhu666/easy_table
我最近在GitHub上开始了一个项目,试一试:https://github.com/cthulhu666/easy_table