Class: Munger::Render::SortableHtml
- Inherits:
-
Object
- Object
- Munger::Render::SortableHtml
- Defined in:
- lib/munger/render/sortable_html.rb
Overview
Render a table that lets the user sort the columns
Instance Attribute Summary collapse
-
#classes ⇒ Object
readonly
Returns the value of attribute classes.
-
#report ⇒ Object
readonly
Returns the value of attribute report.
Instance Method Summary collapse
- #cycle(one, two) ⇒ Object
-
#initialize(report, options = {}) ⇒ SortableHtml
constructor
options: :url => /some/url/for/link # link to put on column :params => :url_params # parameters from url if any :sort => ‘column’ # column that is currently sorted :order => ‘asc’ || ‘desc’ # order of the currently sorted field.
- #render ⇒ Object
- #set_classes(options = nil) ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(report, options = {}) ⇒ SortableHtml
options: :url => /some/url/for/link # link to put on column :params => :url_params # parameters from url if any :sort => ‘column’ # column that is currently sorted :order => ‘asc’ || ‘desc’ # order of the currently sorted field
14 15 16 17 18 19 20 21 |
# File 'lib/munger/render/sortable_html.rb', line 14 def initialize(report, = {}) @report = report = # default url and params options [:url] ||= '/' [:params] ||= {} set_classes([:classes]) end |
Instance Attribute Details
#classes ⇒ Object (readonly)
Returns the value of attribute classes.
7 8 9 |
# File 'lib/munger/render/sortable_html.rb', line 7 def classes @classes end |
#report ⇒ Object (readonly)
Returns the value of attribute report.
7 8 9 |
# File 'lib/munger/render/sortable_html.rb', line 7 def report @report end |
Instance Method Details
#cycle(one, two) ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/munger/render/sortable_html.rb', line 110 def cycle(one, two) if @current == one @current = two else @current = one end end |
#render ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/munger/render/sortable_html.rb', line 29 def render x = Builder::XmlMarkup.new x.table(:class => @classes[:table]) do x.tr do @report.columns.each do |column| # TODO: Should be able to see if a column is 'sortable' # Assume all columns are sortable here - for now. sorted_state = 'unsorted' direction = 'asc' if [column.to_s, @report.column_data_field(column)].include?([:sort]) sorted_state = "sorted" direction = [:order] == 'asc' ? 'desc' : 'asc' direction_class = "sorted-#{direction}" end new_params = [:params].merge({'sort' => @report.column_data_field(column),'order' => direction}) x.th(:class => "columnTitle #{sorted_state} #{direction_class}" ) do # x << @report.column_title(column) x << "<a href=\"#{@options[:url]}?#{create_querystring(new_params)}\">#{@report.column_title(column)}</a>" end end end @report.process_data.each do |row| classes = [] classes << row[:meta][:row_styles] classes << 'group' + row[:meta][:group].to_s if row[:meta][:group] classes << cycle('even', 'odd') classes.compact! if row[:meta][:group_header] classes << 'groupHeader' + row[:meta][:group_header].to_s end row_attrib = {} row_attrib = {:class => classes.join(' ')} if classes.size > 0 x.tr(row_attrib) do if row[:meta][:group_header] header = @report.column_title(row[:meta][:group_name]) + ' : ' + row[:meta][:group_value].to_s x.th(:colspan => @report.columns.size) { x << header } else @report.columns.each do |column| cell_attrib = {} if cst = row[:meta][:cell_styles] cst = Item.ensure(cst) if cell_styles = cst[column] cell_attrib = {:class => cell_styles.join(' ')} end end # TODO: Clean this up, I don't like it but it's working # output the cell # x.td(cell_attrib) { x << row[:data][column].to_s } x.td(cell_attrib) do formatter,*args = *@report.column_formatter(column) col_data = row[:data] #[column] if formatter && col_data[column] formatted = if formatter.class == Proc formatter.call(col_data.data) elsif col_data[column].respond_to? formatter col_data[column].send(formatter, *args) elsif col_data[column].to_s end else formatted = col_data[column].to_s end x << formatted.to_s end end end end end end end |
#set_classes(options = nil) ⇒ Object
23 24 25 26 27 |
# File 'lib/munger/render/sortable_html.rb', line 23 def set_classes( = nil) = {} if ! default = {:table => 'report-table'} @classes = default.merge() end |
#valid? ⇒ Boolean
118 119 120 |
# File 'lib/munger/render/sortable_html.rb', line 118 def valid? @report.is_a? Munger::Report end |