UsefullTable
table_for generate a full-optionals table, with excel export, columns ordering, links, inline editing and monitoring (ActsAsMonitor github.com/skylord73/acts_as_monitor) but don’t worry because a rich set of defaults, make its use very simple!
for a working sample please refer to test/dummy
Setup
Add this line to your application’s Gemfile:
gem 'usefull_table'
then execute
$ bundle install
or install it yourself as:
$ sudo gem install usefull_table
copy icons, javascript and stylesheets:
$ rails g usefull_table:install
modify default scaffold to use usefull_table, meta_search and will_paginate (controller and index.html.erb):
$ rails g usefull_table:scaffold
Usage
Write few lines in your controller
app/controllers/home_controller.rb
def index
@search = Item.search(params[:search])
...
respond_to do |format|
format.html { @items = @search.paginate(:page => params[:page]) }
end
...
end
and in your view
app/views/home/my_view.html.erb
<%= table_for @items, @search, options = {} do |t| %>
<% t.show :url => Proc.new { |item| item_path(item)} %>
<% t.edit :url => Proc.new { |item| edit_item_path(item)}%>
<% t.destroy :url => Proc.new { |item| item_path(item)}, :link_options => {:method => delete, :confirm => "are you sure?"} %>
<% t.download :url => Proc.new { |item| download_item_path(item)} %>
<% t.col :name %>
<% t.col "user.name" %>
<% t.status %>
<% end %>
Options
default values in bold
Paginator
options[:paginator][:visible] = *true* | false _note_: false if @items not present
options[:paginator][:class] = *"usefull_table_paginator"*
Container
options[:html] = *{:class => "usefull_table_container"}*
Excel
options[:export][:visible] = *true* | false
options[:export][:filter] = *true* | false _note:_ false if @search not present
options[:export][:human] = *true* | false
options[:export][:worksheet] = *object.class.name.gsub(/::/,"#")* _note:_ class name with namespace separator #
options[:export][:url] = Custom Url and format
Table
options[:table][:div_html] = *{:class => "usefull_table"}*
options[:table][:header_html] = *{:class => "first_row"}*
options[:table][:header_type] = *:sort* _note:_ :human if @search not present (no sorting possible)
:plain bare column name from ActiveRecord
:human column name humanized by ActiveRecord
:nil no column name
Localization
Uses standard ActiveRecord localization to render tables and columns names
it:
activerecord:
attributes:
item:
name: Name
type: Type
user:
name: Name
models:
item:
one: Item
other: Items
user:
one: User
other: Users
#config/usefull_table.it.yml
it:
usefull_table:
submit_excel: Excel
header_error: Errore
body_error: Errore
icons:
show: "usefull_table_show.png"
edit: "usefull_table_edit.png"
destroy: "usefull_table_destroy.png"
download: "usefull_table_download.png"
Column Types
col
Render column value
Usage
<% t.col :name %> #render column :name ( t.col "name" is ok)
<% t.col "user.name" %> #render column name of the user collection in item (item.user.name)
Options
:header_type =>
-
:sort #Header is MetaSearch#sort_link of columns_name
-
:human #Header is plain text humanized with ActiveRecord column name
-
:nil #No header
:label =>
-
“Custom Name” #plain text without localization
-
:custom_name #localized text in lazy context (.)
:data_type => #default get class name from object to render
-
:Date
-
:Time
-
:DateTime
-
:Currency
-
:Percentage #transform float number in percentage with :precision => 0 ( 0.1 => 10%)
-
:Bool #Transform value in boolean
-
:Bool_reverse #Transform vale in boolean and reverse the vale
:url =>
-
“static_path”
-
Proc #Proc expose the object instance of the current row
:inline =>
-
false
-
true #enable inline editing for the column, works also with nested fields (no controller add-on required)
label
-
Render static label
-
<% t.label Proc.new {|item| item.id * 3}, :label => “Id * 3” %>
Usage
<% t.label object %> #render object.inspect
<% t.label Proc.new {|item| item.name.capitalize} %> #Evaluate proc with item instance of the corresponding row
monitor
Render a tri-state icon to monitor model status
-
Red : Error
-
Yellow: Warning
-
Green: Status ok
Enable only if acts_as_monitor gem is required (github.com/skylord73/acts_as_monitor)
Usage
<% t.monitor %>
Clicking the icon you get the comlete problem description pushed by Ajaxs script (no page reload)
link
Create a link to something, using Icons or CustomText
Usage
<% t.show :url => Proc.new {|object| home_path(object) }"%>
<% t.destroy :url => Proc.new {|object| home_path(object) }"%>
<% t.link :name => "Custom Link", :url => Proc.new {|object| my_link_home_path(object) }"%> #text link (Custom Link) to url
<% t.link :name => "Custom Link", :body_typ => :icon, :url => Proc.new {|object| my_link_home_path(object) }"%> #icon link with icon name = usefull_table_cusom_link.png or localization in usefull_table.icons.custom_link
Options
:url =>
-
Proc
-
“my_custom_static_url”
:label =>
-
:show_doc #localized in lazy contest (.)
-
“Show Doc” #printed without localization
:link_options =>
-
nil
-
=> delete, :confirm => “sicuro?” if name == :destroy
:name =>
-
:symbol
-
“string” #if method_name == :link the name is used as link_text (localized if symbol), ignored elsewhere
bool
Create a boolean value using images to represent status. Images name can be changed in locales
Usage
<% t.bool :name %>
<% t.bool :name, :reverse => true%>
Options
:header_type =>
-
:sort #Header is MetaSearch#sort_link of columns_name
-
:human #Header is plain text humanized with ActiveRecord column name
-
:nil #No header
:label =>
-
“Custom Name” #plain text without localization in header
-
:custom_name #localized text in lazy context (.) in header
:url =>
-
“my_custom_static_url”
-
Proc #Proc expose the object instance of the current row
:reverse =>
-
false
-
true #reverse boolean value
Personalization
Feel free to modify the following files:
-
public/stylesheets/usefull_table.css
-
config/locales/usefull_table.it.yml
Contributing
-
Fork it
-
Create your feature branch (git checkout -b my-new-feature)
-
Commit your changes (git commit -am ‘Added some feature’)
-
Push to the branch (git push origin my-new-feature)
-
Create new Pull Request
Thanks
Many thanks to :
-
MetaSearch
-
WillPaginate
-
Spreadsheet
-
Axlsx