# To achieve inline crud, we use rails' data-remote links, and override their behaviour when inside a datatable # This works with EffectiveForm.remote_form which is part of the effective_bootstrap gem. # We click the New/Edit/Action button from the col-actions $(document).on 'ajax:beforeSend', '.dataTables_wrapper .col-actions', (e, xhr, settings) -> $action = $(e.target) $table = $(e.target).closest('table') return true if ('' + $action.data('inline')) == 'false' $params = $.param({_datatable_id: $table.attr('id'), _datatable_cookie: $table.data('cookie') }) settings.url += (if settings.url.indexOf('?') == -1 then '?' else '&') + $params if $action.closest('.effective-datatables-inline-row').length > 0 # Nothing. else if $action.closest('tr').parent().prop('tagName') == 'THEAD' beforeNew($action) else beforeEdit($action) true # We have either completed the resource action, or fetched the inline form to load. $(document).on 'ajax:success', '.dataTables_wrapper .col-actions', (event) -> $action = $(event.target) return true if ('' + $action.data('inline')) == 'false' if ($action.data('method') || 'get') == 'get' if $action.closest('tr').parent().prop('tagName') == 'THEAD' then afterNew($action) else afterEdit($action) else afterAction($action) EffectiveForm.remote_form_payload = '' EffectiveForm.remote_form_flash = '' EffectiveForm.remote_form_refresh_datatables = '' true # There was an error completing something $(document).on 'ajax:error', '.dataTables_wrapper', (event) -> $action = $(event.target) $table = $action.closest('table') $table.DataTable().flash('unable to ' + ($action.attr('title') || 'complete action'), 'danger').draw() EffectiveForm.remote_form_payload = '' EffectiveForm.remote_form_flash = '' EffectiveForm.remote_form_refresh_datatables = '' true # Submitting an inline datatables form $(document).on 'ajax:beforeSend', '.dataTables_wrapper .col-inline-form', (e, xhr, settings) -> $table = $(e.target).closest('table') $params = $.param({_datatable_id: $table.attr('id'), _datatable_cookie: $table.data('cookie') }) settings.url += (if settings.url.indexOf('?') == -1 then '?' else '&') + $params true # The inline form has been submitted successfully $(document).on 'effective-form:success', '.dataTables_wrapper .col-inline-form', (event, flash) -> $action = $(event.target) $tr = $action.closest('tr') $table = $tr.closest('table') if $tr.hasClass('effective-datatables-new-resource') $table.DataTable().flash(flash || 'Item created', 'success').draw() $tr.fadeOut('slow') $actions = $table.children('thead').find('th.col-actions') $actions.children('svg').remove() $actions.children('a').fadeIn() else $table.DataTable().flash(flash || 'Item updated', 'success').draw() $tr.fadeOut('slow') refreshDatatables($table) beforeNew = ($action) -> $table = $action.closest('table') $th = $action.closest('th') # Hide New Button $th.children('a').hide() # Append spinner and show Processing $th.append($table.data('spinner')) $table.DataTable().flash() $table.one 'draw.dt', (event) -> $th.find('a').show().siblings('svg').remove() afterNew = ($action) -> $tr = $action.closest('tr') $table = $tr.closest('table') $action.siblings('svg').remove() html = buildRow($tr.children('th').length, EffectiveForm.remote_form_payload) $tr = $("