Module: Daru::View::Adapter::DatatablesAdapter

Extended by:
DatatablesAdapter
Included in:
DatatablesAdapter
Defined in:
lib/daru/view/adapters/datatables.rb

Instance Method Summary collapse

Instance Method Details

#export_html_file(table, path = './table.html') ⇒ void

This method returns an undefined value.

Returns writes the html code of the datatable to the file.

Examples:

table = Daru::View::Table.new(data, options)
table.export_html_file

Parameters:

  • table (Daru::DataTables::DataTable)

    table object to access daru-data_table methods



85
86
87
88
89
# File 'lib/daru/view/adapters/datatables.rb', line 85

def export_html_file(table, path='./table.html')
  path = File.expand_path(path, Dir.pwd)
  str = generate_html(table)
  File.write(path, str)
end

#generate_body(table) ⇒ String

Returns script and table (containg thead only) tags of the datatable generated.

Parameters:

  • table (Daru::DataTables::DataTable)

    table object to access daru-data_table methods

Returns:

  • (String)

    script and table (containg thead only) tags of the datatable generated



75
76
77
# File 'lib/daru/view/adapters/datatables.rb', line 75

def generate_body(table)
  table.to_html
end

#generate_html(table) ⇒ String

Returns html code of the datatable generated

Parameters:

  • table (Daru::DataTables::DataTable)

    table object to access daru-data_table methods

Returns:

  • (String)

    returns html code of the datatable generated



104
105
106
107
108
109
110
111
112
113
# File 'lib/daru/view/adapters/datatables.rb', line 104

def generate_html(table)
  path = File.expand_path(
    '../templates/datatables/static_html.erb', __dir__
  )
  template = File.read(path)
  table_script = generate_body(table)
  initial_script = init_script
  id = table.element_id
  ERB.new(template).result(binding)
end

#init_irubyvoid

This method returns an undefined value.

Returns loads the dependent JS and CSS files in IRuby notebook.

Examples:

table = Daru::View::Table.new(data, options)
table.init_iruby


119
120
121
# File 'lib/daru/view/adapters/datatables.rb', line 119

def init_iruby
  Daru::View::DataTables.init_iruby
end

#init_scriptString

Returns code of the dependent JS and CSS file(s)

Returns:

  • (String)

    returns code of the dependent JS and CSS file(s)



67
68
69
# File 'lib/daru/view/adapters/datatables.rb', line 67

def init_script
  Daru::View::DataTables.init_script
end

#init_table(data = [], options = {}, _user_options = {}) ⇒ Daru::DataTables::DataTable

Read : datatables.net/ to understand the datatables option concept. Along with these options, a user can provide an additional option

html_options[:table_options] to cistomize the generated table

See the specs of daru-data_tables gem.

Examples:

DataTable

Set Daru::View.table_library = :googlecharts (or set adapter option)
  (Also set Daru::View.dependent_script(:googlecharts) in web
  frameworks in head tag)
Formulate the data to visualize
  idx = Daru::Index.new ['Year', 'Sales']
  data_rows = [
                ['2004',  1000],
                ['2005',  1170],
                ['2006',  660],
                ['2007',  1030]
              ]
  df_sale_exp = Daru::DataFrame.rows(data_rows)
  df_sale_exp.vectors = idx

Set the options required
  options1 =  {
                 html_options: {
                   table_options: {
                     table_thead: "<thead>
                                    <tr>
                                    <th></th>
                                    <th>C1</th>
                                    <th>C2</th>
                                   </tr>
                                 </thead>",
                     width: '90%'
                   }
                 },
                 scrollX: true
              }
  options2 = {searching: false}

Draw the Daru::View::Table object.
  table = Daru::View::Table.new(df_sale_exp, options1)
  table2 = Daru::View::Table.new(df_sale_exp, options2)
  table3 = Daru::View::Table.new(df_sale_exp)

Parameters:

  • data (Array, Daru::DataFrame, Daru::Vector) (defaults to: [])

    The data provided by the user to generate the datatable

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

    Various options provided by the user to incorporate in datatable

Returns:

  • (Daru::DataTables::DataTable)

    Returns the datatble object



61
62
63
64
# File 'lib/daru/view/adapters/datatables.rb', line 61

def init_table(data=[], options={}, _user_options={})
  @table = Daru::View::DataTable.new(data, options)
  @table
end

#show_in_iruby(table) ⇒ void

This method returns an undefined value.

Returns shows the datatable in IRuby notebook.

Examples:

table = Daru::View::Table.new(data, options)
table.show_in_iruby

Parameters:

  • table (Daru::DataTables::DataTable)

    table object to access daru-data_table methods



97
98
99
# File 'lib/daru/view/adapters/datatables.rb', line 97

def show_in_iruby(table)
  table.show_in_iruby
end