module DT
Public Class Methods
array_hash_to_table(array_hash)
click to toggle source
++ construct html table from array, but all the elements are same shape hash, array = [{“name” => “John”, “year” => “1990”},{“name” => “Jim”, “year” => “1991”}] ++
# File lib/dt.rb, line 62 def self.array_hash_to_table(array_hash) # all the element of array are same structure hash # table_head = "<table border=\"1\"><thead><tr>" table_head_cell = String.new array_hash[0].each_key do |key| table_head_cell += "<th>" + key + "</th>" end table_head += table_head_cell + "</tr></thead>" table_body = "" table_cell = "" array_hash.each do |hash| table_cell += "<tr>" hash.each do |key, value| table_cell += "<td>#{value}</td>" end table_cell += "</tr>" end table_body = "<tbody>" + table_cell + "</tbody>" table_head + table_body end
array_to_list(array)
click to toggle source
++ construct html table from array ++
# File lib/dt.rb, line 5 def self.array_to_list(array) list_string_start = "<ul>" list_string_content = String.new array.each do |item| list_string_content += "<li>" + item + "</li>" end list_string_end = "</ul>" list_string_start + list_string_content + list_string_end end
hash_array_to_table(hash_array)
click to toggle source
++ construct html table from a hash with all the value should be array like, hash => {“name” => [“John”, “Jim”], “Year” => [“1990”, “1991”]} ++
# File lib/dt.rb, line 19 def self.hash_array_to_table(hash_array) table_head = "<table border=\"1\"><thead><tr>" table_head_cell = "" len = 0 key_num = hash_array.keys.length hash_array.each_key do |key| table_head_cell += "<th>" + key + "</th>" new_len = hash_array["#{key}"].length if new_len > len len = new_len end end table_head_cell += "</tr></thead>" table_body = String.new i = 0 table_body = "<tbody>" while (i < len) sub_head = "<tr>" sub_body = "" j = 0 key_num.times do value = hash_array["#{hash_array.keys[j]}"][i] sub_body += "<td>#{value}</td>" if value sub_body += "<td></td>" if !value j += 1 end i += 1 sub_foot = "</tr>" sub = sub_head + sub_body + sub_foot table_body += sub end table_head + table_head_cell + table_body + "</tbody>" end