Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module TableHelper
- # Table helper allow easy generate simple table layout
- # items - collection of variables
- # options:
- # you can define any attribute for table in this parameter
- # ex. :style => 'border: 1px solid black', :align => 'center'
- # You can ovveride predefined next parameters:
- # cols: count of columns in a table
- # rows: count of rows in a table. The cols parameter will be disabled in this case.
- # tr: options for a tr tag, ex.: :tr => {:class => 'block', :width => '100px'}
- # td: options for a td tag. You can assign false on td parameter, it's mean helper will not generate td tags, only tr.
- # Examples of use:
- # <% table @genres, :cols => 3, :td => {:width => '200px'} do |genre| -%>
- # <i><%= genre.name %></i>
- # <% end %>
- #
- # <% table @genres, :td => false do |genre| -%>
- # <td><%= genre.id %></td>
- # <td><%= genre.name %></td>
- # <% end %>
- def table(items, options, &block)
- cols = options[:cols] || 1
- cols = items.size / options[:rows].to_i if options[:rows]
- tr_options = options[:tr]
- td_options = options[:td]
- options[:cols] = nil
- options[:rows] = nil
- options[:tr] = nil
- options[:td] = nil
- if td_options != false
- trs = ""
- items.each_slice(cols) do |slice|
- # generate td tags for current tr tag
- tds = slice.inject("") do |tds, item|
- tds + content_tag(:td, capture(item, &block), td_options)
- end
- trs += content_tag(:tr, tds, tr_options)
- end
- else
- trs = items.inject(""){|trs, item| trs + content_tag(:tr, capture(item, &block), tr_options) }
- end
- # send generated table to view
- concat(content_tag(:table, trs, options))
- end
- end
Add Comment
Please, Sign In to add comment