Module: Daru::View::JsHelpers
- Includes:
- ParamHelpers
- Included in:
- DataTable
- Defined in:
- lib/daru/data_tables/generate_js/generate_js.rb
Instance Method Summary collapse
-
#draw_ajax_option ⇒ void
Adds serverSide and ajax options.
-
#draw_js(element_id) ⇒ String
Generates JavaScript function for rendering the Table.
-
#draw_js_iruby(element_id) ⇒ String
Generates JavaScript function for rendering the Table in IRuby notebook.
-
#extract_data_array ⇒ Array, void
Returns data Array if present in the options.
-
#set_callback_ajax ⇒ String
Returns the callback js from the server.
Methods included from ParamHelpers
Instance Method Details
#draw_ajax_option ⇒ void
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.
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 [:data] && [: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 [: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.
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 [:data] && [: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 [: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_array ⇒ 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 .delete(:data) unless [:data].nil? end |
#set_callback_ajax ⇒ 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 |