Skip to content

Commit 671812e

Browse files
committed
Adding render_simple_datatable() helper
1 parent ee79d94 commit 671812e

File tree

5 files changed

+52
-13
lines changed

5 files changed

+52
-13
lines changed

app/assets/javascripts/effective_datatables/initialize.js.coffee

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ initializeDataTables = ->
33
unless $.fn.DataTable.fnIsDataTable(this)
44
datatable = $(this)
55

6-
datatable.dataTable
6+
init_options =
77
bServerSide: true
88
bProcessing: true
99
bSaveState: true
@@ -12,7 +12,7 @@ initializeDataTables = ->
1212
order: datatable.data('default-order')
1313
sAjaxSource: datatable.data('source')
1414
pagingType: 'simple_numbers'
15-
aLengthMenu: [[10, 25, 50, 100, 250, 1000, -1], [10, 25, 50, 100, 250, 1000, 'All']]
15+
lengthMenu: [[10, 25, 50, 100, 250, 1000, -1], [10, 25, 50, 100, 250, 1000, 'All']]
1616
fnServerParams: (aoData, a, b) ->
1717
table = this.DataTable()
1818
table.columns().flatten().each (index) ->
@@ -39,10 +39,22 @@ initializeDataTables = ->
3939
fnStateChange: (iCol, bVisible) ->
4040
table = $(this.dom.button).closest('.dataTables_wrapper').children('table').first().DataTable()
4141
table.draw()
42-
.columnFilter
43-
sPlaceHolder: 'head:after'
44-
aoColumns : datatable.data('filter')
45-
bUseColVis: true
42+
43+
simple = datatable.data('effective-datatables-table') == 'simple'
44+
filter = datatable.data('filter')
45+
46+
if simple
47+
init_options['lengthMenu'] = [-1] # Show all results
48+
init_options['dom'] = "<'row'r>t" # Just show the table
49+
50+
# Actually initialize it
51+
datatable = datatable.dataTable(init_options)
52+
53+
if filter
54+
datatable.columnFilter
55+
sPlaceHolder: 'head:after'
56+
aoColumns : datatable.data('filter')
57+
bUseColVis: true
4658

4759
$('.dataTables_filter').each ->
4860
$(this).html("<button class='btn-reset-filters ColVis_Button' data-effective-datatables-reset-filters='true'><span>Reset Filters</span></button>")

app/assets/stylesheets/effective_datatables/_overrides.scss

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,25 @@
66
margin-right: 0px;
77
}
88

9+
table.dataTable.sorting-hidden {
10+
thead {
11+
.sorting_asc { background: none; cursor: default; }
12+
.sorting_desc { background: none; cursor: default; }
13+
.sorting { cursor: default; }
14+
15+
tr, th {
16+
padding-left: 8px;
17+
padding-right: 8px;
18+
}
19+
}
20+
}
21+
922
table.dataTable {
1023
border-collapse: collapse;
1124
box-sizing: border-box;
1225

1326
.form-inline, .form-control { width: 100%; }
27+
thead, tr, th { padding-left: 6px; }
1428
}
1529

1630
.dataTables_processing {

app/helpers/effective_datatables_helper.rb

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
module EffectiveDatatablesHelper
2-
def render_datatable(datatable)
2+
def render_datatable(datatable, opts = {})
33
datatable.view = self
4-
render :partial => 'effective/datatables/datatable', :locals => {:datatable => datatable}
4+
locals = {:style => :full, :filterable => true, :sortable => true, :table_class => 'table-bordered table-striped'}.merge(opts)
5+
locals[:table_class] = 'sorting-hidden ' + locals[:table_class].to_s if locals[:sortable] == false
6+
7+
render :partial => 'effective/datatables/datatable', :locals => locals.merge(:datatable => datatable)
8+
end
9+
10+
def render_simple_datatable(datatable, opts = {})
11+
datatable.view = self
12+
locals = {:style => :simple, :filterable => false, :sortable => false, :table_class => ''}.merge(opts)
13+
locals[:table_class] = 'sorting-hidden ' + locals[:table_class].to_s if locals[:sortable] == false
14+
15+
render :partial => 'effective/datatables/datatable', :locals => locals.merge(:datatable => datatable)
516
end
617

7-
def datatable_filter(datatable)
18+
def datatable_filter(datatable, filterable = true)
19+
return false unless filterable
20+
821
filters = datatable.table_columns.values.map { |options, _| options[:filter] || {:type => 'null'} }
922

1023
# Process any Procs
@@ -21,9 +34,9 @@ def datatable_filter(datatable)
2134
filters.to_json()
2235
end
2336

24-
def datatable_non_sortable(datatable)
37+
def datatable_non_sortable(datatable, sortable = true)
2538
[].tap do |nonsortable|
26-
datatable.table_columns.values.each_with_index { |options, x| nonsortable << x if options[:sortable] == false }
39+
datatable.table_columns.values.each_with_index { |options, x| nonsortable << x if options[:sortable] == false || sortable == false }
2740
end.to_json()
2841
end
2942

app/views/effective/datatables/_datatable.html.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
%table.effective-datatable.table-striped.table-bordered{:id => "#{datatable.to_param}-table", :data => {'effective-datatables-table' => true, 'source' => effective_datatables.datatable_path(datatable, {:format => 'json'}.merge(:attributes => datatable.attributes)), 'filter' => datatable_filter(datatable), 'non-sortable' => datatable_non_sortable(datatable), 'non-visible' => datatable_non_visible(datatable), 'widths' => datatable_widths(datatable), 'default-order' => datatable_default_order(datatable)}}
1+
%table.effective-datatable{:id => "#{datatable.to_param}-table", :class => ('table ' + table_class.to_s), :data => {'effective-datatables-table' => style, 'source' => effective_datatables.datatable_path(datatable, {:format => 'json'}.merge(:attributes => datatable.attributes)), 'filter' => datatable_filter(datatable, filterable), 'non-sortable' => datatable_non_sortable(datatable, sortable), 'non-visible' => datatable_non_visible(datatable), 'widths' => datatable_widths(datatable), 'default-order' => datatable_default_order(datatable)}}
22
%thead
33
- max_depth = datatable.table_columns.map { |_, opts| opts[:th][:depth].to_i rescue 0 }.max
44
- [*0..max_depth].each do |depth|

lib/effective_datatables/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module EffectiveDatatables
2-
VERSION = "0.3.17"
2+
VERSION = "0.3.18"
33
end

0 commit comments

Comments
 (0)