Module: DataTablesHelper

Defined in:
lib/data_tables/data_tables_helper.rb

Instance Method Summary collapse

Instance Method Details

#datatables(source, opts = {}) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/data_tables/data_tables_helper.rb', line 2

def datatables(source, opts = {})

  options = opts[:jquery] ? opts[:jquery].dup : {}
  options[:bJQueryUI] = true unless options.has_key?(:bJQueryUI)
  options[:bProcessing] = true unless options.has_key?(:bProcessing)
  options[:bServerSide] = true unless options.has_key?(:bServerSide)
  options[:bAutoWidth] = false unless options.has_key?(:bAutoWidth)
  options[:bStateSave] = true unless options.has_key?(:bStateSave)
  options[:oColVis] ||= {}
  options[:bFilter] = true
  options[:oColVis][:aiExclude] ||= []
  unless options[:oColVis][:aiExclude].include?(0)
    options[:oColVis][:aiExclude].unshift(0)
  end

  options[:bFilter] = opts[:search] unless opts[:search].nil?

  options[:fnInitComplete] ||= "function() {
    if (eval('typeof ' + initDatatablesTable) == 'function') {
      initDatatablesTable('#{source}');
    }
  }"

  sdom = options[:bFilter] ? '<"#datatables_search_hint">lfrtip' : 'lrtip'
  sdom = "C<\"clear\">" + sdom if options[:oColVis]
  sdom = 'T' + sdom if options[:oTableTools]
  options[:sDom] ||= sdom

  # Rails.logger.info("*****#{options.inspect}")

  # options[:sDom].delete(:f)

  datatable = controller.datatable_source(source)
  url_query_params = opts[:urlQueryParams] || {}
  options[:sAjaxSource] = opts[:sAjaxSource] ||
      method("#{datatable[:action]}_url".to_sym).call(url_query_params)
  columns = datatable[:attrs].keys.collect { |a| "<th>#{a}</th>" }.join

  index = 0
  first_searchable_column_index = nil
  targets = datatable[:attrs].inject([]) do |memo, (column, searchable)|
    first_searchable_column_index ||= index if searchable
    memo << index unless searchable
    index += 1
    memo
  end
  options[:aaSorting] ||= [[first_searchable_column_index, 'asc']]
  options[:aoColumnDefs] ||= []
  options[:aoColumnDefs].unshift({
                                   :aTargets => targets,
                                   :bSearchable => false,
                                   :bSortable => false
                                 })

  if options[:html]
    html_opts = options[:html].collect { |k,v| "#{k}=\"#{v}\"" }.join(' ')
  end
  pad_ao_columns(options, datatable[:attrs].keys.size)

  table_header = "<tr>#{columns}</tr>"
  html = "
<script>
$(document).ready(function() {
var oTable = $('##{datatable[:action]}').dataTable({
#{datatables_option_string(options)}
});
$('tfoot input').keyup( function () {
              /* Filter on the column (the index) of this element */
              oTable.fnFilter( this.value, $('tfoot input').index(this) );
      } );

});
</script>
<table id=\"#{datatable[:action]}\" #{html_opts}>
<thead>
#{table_header}
</thead>
<tbody>
</tbody>
</table>
"
  return raw(html)
end