ramanr
ramanr

Reputation: 735

Table component in rails

I'm looking for a simple table component to use in my rails application. I'll need filters & page navigation on that.

To explain more, I've quite a few table views in my app for various models, instead of coming up with markup separately, I would like to have a table_for similar to form_for. Also the table_for should be capable of rendering data from associations too.

Will be great if there is anything out there. There is something like what I want here - http://www.shaneharvie.com/2007/03/tablefor-erb-template.html. It has to be improvised to suit my needs.

Upvotes: 0

Views: 734

Answers (1)

nathanvda
nathanvda

Reputation: 50057

A component I am very happy with, is wice_grid. It builds the query, pagination, .. for you, allows to filter on columns very easily.

A simple example (from the documentation) : suppose you have a model Task, and you want the build the index/table. Then in the controller you write the following:

@tasks_grid = initialize_grid(Task)

... and in the View:

<%= grid(@tasks_grid) do |g|
  g.column :column_name => 'ID', :attribute_name => 'id'
  g.column :column_name => 'Title', :attribute_name => 'title'
  g.column  :column_name => 'Description', :attribute_name => 'description'
  g.column  :column_name => 'Archived', :attribute_name => 'archived' do |task|
    task.archived? ? 'Yes' : 'No'
  end

  g.column   do |task|
    link_to('Edit', edit_task_path(task))
  end
end -%>

Hope this helps.

Upvotes: 1

Related Questions