Pergunta

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

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"

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?

Foi útil?

Solução

@ChrisH: Representing table using two arrays won't give more control. I would suggest the following: table_helper

erb snippet -

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

Outras dicas

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.

This is the one I ended up using in the same situation:

https://github.com/lunich/table_for

Try Datagrid - ruby library that helps you to build and represent table-like data with:

  • Customizable filtering
  • Columns
  • Sort order
  • Localization
  • Export to CSV

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.

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

I recently started a project on GitHub, give it a try: https://github.com/cthulhu666/easy_table

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top