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

Instance Method Summary collapse

Constructor Details

#initialize(engine) ⇒ TableRenderer

Set up the web server.



24
25
26
27
# File 'lib/gloo/web_svr/table_renderer.rb', line 24

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.



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

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 style='#{styles[ HEAD_CELL ]}'>#{head[ :title ]}</th>"
    str += "<td style='#{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



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/gloo/web_svr/table_renderer.rb', line 45

def data_to_table params
  data = params[ :rows ]
  
  if data.nil? || ( data.length == 0 )
    return "<p>No data found.</p>"
  elsif data.length == 1
    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.



88
89
90
91
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
# File 'lib/gloo/web_svr/table_renderer.rb', line 88

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 style='#{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).



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/gloo/web_svr/table_renderer.rb', line 129

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