initializeDataTables = -> $('table[data-effective-datatables-table]').each -> unless $.fn.DataTable.fnIsDataTable(this) datatable = $(this) init_options = ajax: { url: datatable.data('source'), type: 'POST' } autoWidth: false processing: true deferRender: true serverSide: true scrollCollapse: true deferLoading: [datatable.data('display-records'), datatable.data('total-records')] # fixedColumns won't work with deferLoading pagingType: 'simple_numbers' language: { 'lengthMenu': 'Show _MENU_ per page'} lengthMenu: [[10, 25, 50, 100, 250, 1000, -1], ['10', '25', '50', '100', '250', '1000', 'All']] iDisplayLength: datatable.data('default-entries') columnDefs: [ { visible: false, targets: datatable.data('non-visible') }, { sortable: false, targets: datatable.data('non-sortable') } ].concat(datatable.data('column-classes') || []).concat(datatable.data('column-names') || []) order: datatable.data('default-order') colVis: showAll: 'Show all' restore: 'Show default' activate: 'click' align: 'right' label: (index, title, th) -> $(th).prop('title') tableTools: sSwfPath: "<%= asset_path('effective_datatables/copy_csv_xls_pdf.swf') %>", aButtons: ['csv', {'sExtends': 'xls', 'sButtonText': 'Excel'}, 'print'] simple = datatable.data('effective-datatables-table') == 'simple' if simple init_options['lengthMenu'] = [-1] # Show all results init_options['dom'] = "<'row'r>t" # Just show the table # Initialize Searching dataTableSearch = (event) -> # This is the function called by a select or input to run the search obj = $(event.currentTarget) table = obj.closest('table.dataTable') # We're using our own scroll wrapper, not the ScrollX support if table.parent().hasClass('dataTables_scrollHeadInner') # ScrollX support table = table.closest('.dataTables_scroll').children('.dataTables_scrollBody').children('table.dataTable') table.DataTable().column("#{obj.data('column-name')}:name").search(obj.val()).draw() # For every existing Input, Set up the search events (search_inputs = datatable.find('thead').first().find('input,select')).each (index, input) -> $input = $(input) if $input.data('column-name') $input.on 'click', (event) -> false # Dont order columns when you click inside the input $input.on 'mousedown', (event) -> event.stopPropagation() # Dont order columns when you click inside the input if $input.is('select') $input.on 'change', (event) -> dataTableSearch(event) else if $input.is('input') $input.keyup($.debounce(300, dataTableSearch)) # Let's actually initialize the table now table = datatable.dataTable(init_options) # Assign PreSearch columns search_inputs.each (index, input) => $input = $(input) if $input.data('column-index') && $input.data('column-name') && $input.val() table.fnSettings().aoPreSearchCols[$input.data('column-index')].sSearch = $input.val() # FixedColumns doesn't work well yet. # fixedColumns = new $.fn.dataTable.FixedColumns(table, # leftColumns: 2 # ) new $.fn.dataTable.ColReorder(table) $ -> initializeDataTables() $(document).on 'page:change', -> initializeDataTables()