initializeDataTables = -> $('table.effective-datatable').each -> return if $.fn.DataTable.fnIsDataTable(this) datatable = $(this) simple = (datatable.data('simple') == true) input_js_options = datatable.data('input-js-options') || {} init_options = ajax: { url: datatable.data('source'), type: 'POST' } autoWidth: false buttons: [ { extend: 'colvis', text: 'Show / hide columns', postfixButtons: [ { extend: 'colvisGroup', text: 'Show all', show: ':hidden'}, { extend: 'colvisRestore', text: 'Show default'} ] }, { extend: 'copy', exportOptions: format: header: (str) -> $("
#{str}
").children('.filter-label').first().text() columns: ':visible:not(.col-actions)' }, { extend: 'csv', exportOptions: format: header: (str) -> $("
#{str}
").children('.filter-label').first().text() columns: ':visible:not(.col-actions)' }, { extend: 'excel', exportOptions: format: header: (str) -> $("
#{str}
").children('.filter-label').first().text() columns: ':visible:not(.col-actions)' }, { extend: 'print', exportOptions: format: header: (str) -> $("
#{str}
").children('.filter-label').first().text() columns: ':visible:not(.col-actions)' }, ] colReorder: !simple columns: datatable.data('columns') deferLoading: [datatable.data('display-records'), datatable.data('total-records')] deferRender: true iDisplayLength: datatable.data('display-entries') language: { 'lengthMenu': 'Show _MENU_ per page'} lengthMenu: [[10, 25, 50, 100, 250, 1000, -1], ['10', '25', '50', '100', '250', '1000', 'All']] order: datatable.data('default-order') processing: true responsive: true serverParams: (params) -> table = this.api() table.columns().flatten().each (index) => params['columns'][index]['visible'] = table.column(index).visible() serverSide: true scrollCollapse: true pagingType: 'simple_numbers' headerCallback: -> table = this.api() table.columns().flatten().each (index) => $th = $(table.column(index).header()) return if $th.hasClass('initialized') settings = table.settings()[0].aoColumns[index] # column specific settings if settings.filterSelectedValue # Assign preselected filter values table.settings()[0].aoPreSearchCols[index].sSearch = settings.filterSelectedValue if settings.filterHtml # Append the html filter HTML and initialize input events $th.append('
' + settings.filterHtml) initializeFilterEvents($th) $th.addClass('initialized') # Sets up the proper events for each input initializeFilterEvents = (th) -> th.find('input,select').each (_, input) -> $input = $(input) $input.parent().on 'click', (event) -> false # Dont order columns when you click inside the input $input.parent().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)) # Do the actual search dataTableSearch = (event) -> # This is the function called by a select or input to run the search obj = $(event.currentTarget) table = obj.closest('table.dataTable') table.DataTable().column("#{obj.data('column-name')}:name").search(obj.val()).draw() if simple init_options['dom'] = "<'row'<'col-sm-12'tr>>" # Just show the table datatable.addClass('sort-hidden') # Let's actually initialize the table now table = datatable.dataTable(jQuery.extend(init_options, input_js_options)) # Apply EffectiveFormInputs to the Show x per page dropdown if datatable.data('effective-form-inputs') table.closest('.dataTables_wrapper').find('.dataTables_length select').select2() # Capture column visibility changes and refresh datatable datatable.on 'column-visibility.dt', (event, settings, index, state) -> $table = $(event.currentTarget) timeout = $table.data('timeout') clearTimeout(timeout) if timeout $table.data('timeout', setTimeout( => $table.DataTable().draw() $.event.trigger('page:change') , 700) ) $ -> initializeDataTables() $(document).on 'page:change', -> initializeDataTables()