Class: ReportBuilder::Table::HtmlBuilder

Inherits:
ElementBuilder show all
Defined in:
lib/reportbuilder/table/htmlbuilder.rb

Instance Method Summary collapse

Methods inherited from ElementBuilder

#initialize

Constructor Details

This class inherits a constructor from ReportBuilder::ElementBuilder

Instance Method Details

#generateObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/reportbuilder/table/htmlbuilder.rb', line 4

def generate()
  t=@element
  anchor=@builder.table_entry(t.name)
  out="<a name='#{anchor}'></a><table><caption>#{t.name}</caption>"
  @rowspans=[]
  if t.header.size>0
    out+="<thead>"+parse_row(t,t.header,"th")+"</thead>\n"
  end
  out+="<tbody>\n"
  next_class=""
  t.rows.each{|row|
    if row==:hr
      next_class="top"
    else
      class_tag=(next_class=="")?"":" class ='#{next_class}' "
      out+="<tr#{class_tag}>"+parse_row(t,row)+"</tr>\n"
      next_class=""
    end
  }
  out+="</tbody>\n</table>\n"
  @builder.html(out)
end

#parse_row(t, row, tag = "td") ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/reportbuilder/table/htmlbuilder.rb', line 26

def parse_row(t,row,tag="td")
  row_ary=[]
  real_i=0
  row.each_index do |i|
    extra=1 
    while !@rowspans[real_i].nil? and @rowspans[real_i]>0
      @rowspans[real_i]-=1
      row_ary << ""
      real_i+=1
    end
    
    if row[i].is_a? Table::Colspan
      row_ary.push(sprintf("<%s colspan=\"%d\">%s</%s>",tag, row[i].cols, row[i].data,tag))
    elsif row[i].nil?
      row_ary.push("<#{tag}></#{tag}>")
    elsif row[i].is_a? Table::Rowspan
      row_ary.push(sprintf("<%s rowspan=\"%d\">%s</%s>", tag, row[i].rows, row[i].data, tag))
      @rowspans[real_i]=row[i].rows-1
    else
      row_ary.push("<#{tag}>#{row[i]}</#{tag}>")
    end
    real_i+=extra
    
  end
  row_ary.join("")
end