Class: Gloo::WebSvr::TableRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/gloo/web_svr/table_renderer.rb

Constant Summary collapse

TABLE =
'table'.freeze
THEAD =
'thead'.freeze
HEAD_CELL =
'head_cell'.freeze
ROW =
'row'.freeze
CELL =
'cell'.freeze
NO_DATA_FOUND =
"<p>No data found.</p>".freeze

Instance Method Summary collapse

Constructor Details

#initialize(engine) ⇒ TableRenderer

Set up the web server.



26
27
28
29
# File 'lib/gloo/web_svr/table_renderer.rb', line 26

def initialize( engine )
  @engine = engine
  @log = @engine.log
end

Instance Method Details

#data_to_single_row_table(params) ⇒ Object

Show in single-row (form) format.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/gloo/web_svr/table_renderer.rb', line 64

def data_to_single_row_table( params )
  styles = params[ :styles ]
  str = "<table class='#{styles[ TABLE ]}'> <tbody>"
  row = params[ :rows ].first
  
  params[ :columns ].each do |head|
    next unless head[ :visible ]
    cell = row[ head[ :data_index ] ]

    if head[ :cell_renderer ]
      cell_value = render_cell( row, head, params[ :columns ] )
    else
      cell_value = cell
    end

    str += "<tr class='#{styles[ ROW ]}'>"
    str += "<th class='#{styles[ HEAD_CELL ]}'>#{head[ :title ]}</th>"
    str += "<td class='#{styles[ CELL ]}'>#{cell_value}</td>"
    str += "</tr>"
  end

  str += "</tbody></table>"
  return str
end

#data_to_table(params) ⇒ Object

Render the query result set to an HTML table.

params =

head: head, 
cols: result[0],
rows: rows,
styles: self.styles,
cell_renderers: self.cell_renderers



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/gloo/web_svr/table_renderer.rb', line 47

def data_to_table params
  data = params[ :rows ]
  return NO_DATA_FOUND if data.nil? || ( data.length == 0 )

  single_row = true if ( data.length == 1 )
  single_row = false if params[ :always_rows ]
  
  if single_row
    return data_to_single_row_table( params )
  else
    return data_to_table_rows( params )
  end
end

#data_to_table_rows(params) ⇒ Object

Show in normal, multi-row format.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/gloo/web_svr/table_renderer.rb', line 92

def data_to_table_rows( params )
  styles = params[ :styles ]
  # headers = params[ :head ]

  str = "<table class='#{styles[ TABLE ]}'>"
  str << "<thead class='#{styles[ THEAD ]}'><tr>"

  params[ :columns ].each do |head|
    next unless head[ :visible ]
    str += "<th class='#{styles[ HEAD_CELL ]}'>#{head[ :title ]}</th>"
  end
  str << "</tr></thead><tbody>"

  params[ :rows ].each do |row|
    str += "<tr class='#{styles[ ROW ]}'>"

    # row.each_with_index do |cell, i|
    params[ :columns ].each do |head|
      next unless head[ :visible ]

      cell = row[ head[ :data_index ] ]
      this_col_name = head[ :name ]

      if head[ :cell_renderer ]
        cell_value = render_cell( row, head, params[ :columns ] )
      else
        cell_value = cell
      end
      str += "<td class='#{styles[ CELL ]}'>#{cell_value}</td>"
    end
    str += "</tr>"
  end
  str += "</tbody></table>"

  return str
end

#render_cell(row, col, cols) ⇒ Object

Render a cell using the cell renderer and the given context data (the row’s values).



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/gloo/web_svr/table_renderer.rb', line 133

def render_cell row, col, cols
  params = {}

  cols.each_with_index do |c, i|
    params[ c[ :name ] ] = row[ c[ :data_index ] ]
  end

  content = col[ :cell_renderer ]
  content = @engine.running_app.obj.embedded_renderer.render content, params

  # renderer = ERB.new( col[ :cell_renderer ] )
  # content = renderer.result_with_hash( params )

  return content
end