Module: Workbook::Writers::HtmlTableWriter

Included in:
Table
Defined in:
lib/workbook/writers/html_writer.rb

Instance Method Summary collapse

Instance Method Details

#build_cell_options(cell, options = {}) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/workbook/writers/html_writer.rb', line 92

def build_cell_options cell, options={}
  classnames = cell.format.all_names
  classnames = classnames + options[:classnames] if options[:classnames]
  classnames = classnames.join(" ").strip
  td_options = classnames != "" ? {:class=>classnames} : {}
  if options[:data]
    options[:data].each do |key, value|
      td_options = td_options.merge({("data-#{key}".to_sym) => value})
    end
  end
  td_options = td_options.merge({:style=>cell.format.to_css}) if options[:style_with_inline_css] and cell.format.to_css != ""
  td_options = td_options.merge({:colspan=>cell.colspan}) if cell.colspan
  td_options = td_options.merge({:rowspan=>cell.rowspan}) if cell.rowspan
  return td_options
end

#to_html(options = {}) ⇒ String

Generates an HTML table ()

Parameters:

  • options (Hash) (defaults to: {})

    A hash with options

Returns:

  • (String)

    A String containing the HTML code, most importantly ‘:style_with_inline_css` (default false)



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
# File 'lib/workbook/writers/html_writer.rb', line 53

def to_html options={}
  options = {:style_with_inline_css=>false}.merge(options)
  builder = Nokogiri::XML::Builder.new do |doc|
    doc.table do
      doc.thead do
        if header
          doc.tr do
            header.each do |cell|
              th_options = build_cell_options cell, options.merge(classnames: [cell.to_sym], data: {key: cell.to_sym})
              unless cell.value.class == Workbook::NilValue
                doc.th(th_options) do
                  doc.text cell.value
                end
              end
            end
          end
        end
      end
      doc.tbody do
        self.each do |row|
          unless row.header?
            doc.tr do
              row.each do |cell|
                td_options = build_cell_options cell, options
                unless cell.value.class == Workbook::NilValue
                  doc.td(td_options) do
                    doc.text cell.value
                  end
                end
              end
            end
          end
        end
      end
    end
  end
  return builder.doc.to_xhtml
end