Class | Hirb::Helpers::Table |
In: |
lib/hirb/helpers/table/resizer.rb
lib/hirb/helpers/table/filters.rb lib/hirb/helpers/table.rb |
Parent: | Object |
Base Table class from which other table classes inherit. By default, a table is constrained to a default width but this can be adjusted via the max_width option or Hirb::View.width. Rows can be an array of arrays or an array of hashes.
An array of arrays ie [[1,2], [2,3]], would render:
+---+---+ | 0 | 1 | +---+---+ | 1 | 2 | | 2 | 3 | +---+---+
By default, the fields/columns are the numerical indices of the array.
An array of hashes ie [{:age=>10, :weight=>100}, {:age=>80, :weight=>500}], would render:
+-----+--------+ | age | weight | +-----+--------+ | 10 | 100 | | 80 | 500 | +-----+--------+
By default, the fields/columns are the keys of the first hash.
Callback methods can be defined to add your own options that modify rows right before they are rendered. Here‘s an example that allows for searching with a :query option:
module Query # Searches fields given a query hash def query_callback(rows, options) return rows unless options[:query] options[:query].map {|field,query| rows.select {|e| e[field].to_s =~ /#{query}/i } }.flatten.uniq end end Hirb::Helpers::Table.send :include, Query >> puts Hirb::Helpers::Table.render [{:name=>'batman'}, {:name=>'robin'}], :query=>{:name=>'rob'} +-------+ | name | +-------+ | robin | +-------+ 1 row in set
Callback methods:
For a thorough example, see Boson::Pipe.
BORDER_LENGTH | = | 3 |
MIN_FIELD_LENGTH | = | 3 |
CHARS | = | { :top => {:left => '+', :center => '+', :right => '+', :horizontal => '-', :vertical => {:outside => '|', :inside => '|'} }, :middle => {:left => '+', :center => '+', :right => '+', :horizontal => '-'}, :bottom => {:left => '+', :center => '+', :right => '+', :horizontal => '-', :vertical => {:outside => '|', :inside => '|'} } |
filter_any | [RW] | Boolean which sets the default for :filter_any option. |
filter_classes | [RW] | A hash which maps a cell value‘s class to a filter. This serves to set a default filter per field if all of its values are a class in this hash. By default, Array values are comma joined and Hashes are inspected. See the :filter_any option to apply this filter per value. |
last_table | [RW] | Holds last table object created |
Main method which returns a formatted table.
Examples:
Hirb::Helpers::Table.render [[1,2], [2,3]] Hirb::Helpers::Table.render [[1,2], [2,3]], :max_fields=>{0=>10}, :header_filter=>:capitalize Hirb::Helpers::Table.render [['a',1], ['b',2]], :change_fields=>%w{letters numbers}, :max_fields=>{'numbers'=>0.4} Hirb::Helpers::Table.render [{:age=>10, :weight=>100}, {:age=>80, :weight=>500}] Hirb::Helpers::Table.render [{:age=>10, :weight=>100}, {:age=>80, :weight=>500}], :headers=>{:weight=>"Weight(lbs)"} Hirb::Helpers::Table.render [{:age=>10, :weight=>100}, {:age=>80, :weight=>500}], :filters=>{:age=>[:to_f]}