Module: Daru::View::Display

Included in:
DataTable
Defined in:
lib/daru/data_tables/display/display.rb

Instance Method Summary collapse

Instance Method Details

#show_in_iruby(dom = SecureRandom.uuid) ⇒ void

This method returns an undefined value.

Returns shows the datatable in IRuby notebook.

Parameters:

  • dom (String) (defaults to: SecureRandom.uuid)

    The ID of the DIV element that the DataTable should be rendered in



96
97
98
# File 'lib/daru/data_tables/display/display.rb', line 96

def show_in_iruby(dom=SecureRandom.uuid)
  IRuby.html(to_html(dom))
end

#show_script(dom = SecureRandom.uuid, options = {}) ⇒ String

Returns js script to render the table.

Parameters:

  • dom (String) (defaults to: SecureRandom.uuid)

    The ID of the DIV element that the DataTable should be rendered in

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

    options provided

Returns:

  • (String)

    js script to render the table



59
60
61
62
63
64
65
66
67
68
# File 'lib/daru/data_tables/display/display.rb', line 59

def show_script(dom=SecureRandom.uuid, options={})
  script_tag = options.fetch(:script_tag) { true }
  if script_tag
    to_js(dom)
  else
    html = ''
    html << draw_js(dom)
    html
  end
end

#to_html(id = nil, options = {}) ⇒ String

If table_options is not present then it will assume that table tag is already present in the web page source, where we are pasting the html code.

Parameters:

  • id (String) (defaults to: nil)

    The ID of the DIV element that the DataTable should be rendered in

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

    options provided

Returns:

  • (String)

    Generates JavaScript and renders the table in the final HTML output.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/daru/data_tables/display/display.rb', line 78

def to_html(id=nil, options={})
  # More things can be added into table_script.erb
  path = File.expand_path('../templates/table_script.erb', __dir__)
  template = File.read(path)
  id ||= SecureRandom.uuid # TODO: remove it or Use it for table tag.
  element_id ||= id
  table_script = show_script(element_id, script_tag: false)
  html_code = ERB.new(template).result(binding)
  # table_options is given. That means table html code is not present in
  # the webpage. So it must generate table code with given options.
  table_thead = extract_table
  draw_table_thead(element_id, html_code, table_thead)
  html_code
end

#to_js(element_id) ⇒ String

Returns the javascript of the DataTable

Parameters:

  • element_id (String)

    The ID of the DIV element that the DataTable should be rendered in

Returns:

  • (String)

    returns the javascript of the DataTable



103
104
105
106
107
108
109
# File 'lib/daru/data_tables/display/display.rb', line 103

def to_js(element_id)
  js =  ''
  js << "\n<script type='text/javascript'>"
  js << draw_js(element_id)
  js << "\n</script>"
  js
end