Module: Daru::View::JsHelpers

Includes:
ParamHelpers
Included in:
DataTable
Defined in:
lib/daru/data_tables/generate_js/generate_js.rb

Instance Method Summary collapse

Methods included from ParamHelpers

#js_parameters, #typecast

Instance Method Details

#draw_ajax_optionvoid

This method returns an undefined value.

Returns adds serverSide and ajax options.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/daru/data_tables/generate_js/generate_js.rb', line 55

def draw_ajax_option
  ajax_str = ''
  ajax_str << "\nfunction ( data, callback, settings ) {"
  ajax_str << "\n\tvar out = [];"
  ajax_str << "\n\tfor (var i=data.start; i<data.start+data.length; i++) {"
  ajax_str << "\n\t\tif (i < data_array.length) {"
  ajax_str << "\n\t\t\tout.push( data_array[i] );"
  ajax_str << "\n\t\t}"
  ajax_str << "\n\t}"
  ajax_str << "\n\tsetTimeout( function () {"
  ajax_str << set_callback_ajax
  ajax_str << "\n\t}, 50 );"
  ajax_str << "\n}"
  @options[:serverSide] = true
  @options[:ajax] = ajax_str.js_code
end

#draw_js(element_id) ⇒ String

Returns Generates JavaScript function for rendering the Table.

Parameters:

  • id (String)

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

Returns:

  • (String)

    Generates JavaScript function for rendering the Table.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/daru/data_tables/generate_js/generate_js.rb', line 12

def draw_js(element_id)
  if options[:data] && options[:data].length >= 50_000
    data_array = extract_data_array
    draw_ajax_option
  end
  js = ''
  js << "\n$(document).ready(function() {"
  js << "\n"
  js << "\n\tvar data_array = #{data_array};" unless options[:data]
  js << "\n\t$('##{element_id}').DataTable("
  js << "\n\t\t#{js_parameters(@options)}"
  js << "\n\t);"
  js << "\n"
  js << "\n});"
  js
end

#draw_js_iruby(element_id) ⇒ String

Returns Generates JavaScript function for rendering the Table in IRuby notebook.

Parameters:

  • id (String)

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

Returns:

  • (String)

    Generates JavaScript function for rendering the Table in IRuby notebook



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/daru/data_tables/generate_js/generate_js.rb', line 33

def draw_js_iruby(element_id)
  if options[:data] && options[:data].length >= 50_000
    data_array = extract_data_array
    draw_ajax_option
  end
  js = ''
  js << "\n$( function () {"
  js << "\n\tvar data_array = #{data_array};" unless options[:data]
  js << "\n\tvar table = $('##{element_id}').DataTable("
  js << "\n\t\t#{js_parameters(@options)}"
  js << "\n\t);"
  js << "\n"
  js << "\n});"
  js
end

#extract_data_arrayArray, void

Returns data Array if present in the options

Returns:

  • (Array, void)

    returns data Array if present in the options



50
51
52
# File 'lib/daru/data_tables/generate_js/generate_js.rb', line 50

def extract_data_array
  options.delete(:data) unless options[:data].nil?
end

#set_callback_ajaxString

Returns the callback js from the server

Returns:

  • (String)

    returns the callback js from the server



73
74
75
76
77
78
79
80
81
82
# File 'lib/daru/data_tables/generate_js/generate_js.rb', line 73

def set_callback_ajax
  callback_js = ''
  callback_js << "\n\t\tcallback( {"
  callback_js << "\n\t\t\tdraw: data.draw,"
  callback_js << "\n\t\t\tdata: out,"
  callback_js << "\n\t\t\trecordsTotal: data_array.length,"
  callback_js << "\n\t\t\trecordsFiltered: data_array.length,"
  callback_js << "\n\t\t} );"
  callback_js
end