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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# 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[:sScrollY] = "200px"
  options[:sScrollX] = '100%'
  options[:bScrollCollapse] = true
  options[:bDeferRender] = true
  options[:bScrollInfinite] = true
  options[:iDisplayLength] = 100
  options[:bFilter] = true

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

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

  options[:fnServerData] ||= "function ( sSource, aoData, fnCallback ) {
    var init_sSearch = $('#init_sSearch');
    if(init_sSearch != undefined && init_sSearch.val() != '' && init_sSearch.size() != 0) {
      $('.dataTables_filter input').val(init_sSearch.val());
      aoData.push( { name:'sSearch', value: init_sSearch.val() });
      $('#init_sSearch').remove();
    }
    $.ajax( {
              'dataType': 'json',
              'url': sSource,
              'data': aoData,
              'success': fnCallback
              });
    }"

  options[:fnDrawCallback] = "function() {
    change_scrollY();
  }"

  sdom = options[:bFilter] ? '<"#datatables_search_hint">lfrtip' : 'lrtip'
  sdom = "C<\"clear\">" + sdom
  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) );
      } );

});

function change_scrollY() {
  $('.dataTables_scrollHeadInner').css('padding-right','17px');
  $('.dataTables_scrollHeadInner').width('100%');
  $('.dataTable').width('100%');
  $('##{datatable[:action]}').dataTable().fnAdjustColumnSizing(false);
  h = $('##{source}').height();
  if( h > $(window).height() *55/100 )
  {
    $('.dataTables_scrollBody').css('height', ($(window).height() *55/100));
  }
  else
  {
    $('.dataTables_scrollBody').css('height', h+20);
  }
  $('.dataTables_scrollBody').trigger('scroll');
  if($('.dataTable').width() < $('.dataTables_scrollHead').width())
  {
    $('.dataTable').width('100%');
    $('.dataTables_scrollHeadInner').width($('##{source}').width());
  }
}

$(window).resize(change_scrollY);

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