4

In our Rails 3.2 App with rack-pjax enabled appears the following Problem:

  1. You click on a link, a page with datatable in it loads trough pjax. Everything is fine.
  2. You click on antoher link the page "whatever" loads.
  3. You hit the back-button the datatables page loads. BUT the datatable is not working.

Sometimes it loads 2 Tables (with no data in it), sometimes it loads just the old datatable. but its not possible to manipulate the data (searching, go to page 2 etc.). Its completly static.

The Data for the table is served via json (server-side processing) from the rails part of the app. We've already tried to destroy and rebuild the datatable on "pjax:start" or "pjax:end"

Thanks for your Help :)

patrickkeller
  • 1,236
  • 2
  • 11
  • 20
  • try to run the function that creates the table on `ready` event - I mean on simple page load (in addition to creation upon `pjax:start" or "pjax:end`) – Daniel Nov 06 '12 at 10:06
  • I'm experiencing this too. I have my datatables init code on document.ready and on pjax:end and I still see the problem. I tried moving it on pjax:start instead and I don't see the problem anymore, but introduces another problem wherein some of the datatables are not initialized at all. – patsanch Feb 27 '13 at 17:26

1 Answers1

3

I found a working solution hopefully without unforeseen problems.

$(document).ready ->
  initLeagueIndexDataTable()

$(document).on 'pjax:end', ->
  initLeagueIndexDataTable()

initLeagueIndexDataTable : ->
  if ($('#league_index').length > 0 && !$('#league_index_wrapper').length > 0)
    $('#league_index').dataTable({
      'sPaginationType': 'full_numbers',
      'bJQueryUI': true,
      'bProcessing': true,
      'bServerSide': true,
      'sAjaxSource': $('#league_index').data('source'),
      'aoColumnDefs': [
        { "bSortable": false, "aTargets": [ 1 ] },
        { "bSortable": false, "aTargets": [ 2 ] },
        { "bSortable": false, "aTargets": [ 3 ] },
        { "bSortable": false, "aTargets": [ 4 ] },
        { "bSortable": false, "aTargets": [ 5 ] }
      ],
      'bFilter': false,
      'iDisplayLength': 25,
      'bDestroy': true
    })
patsanch
  • 441
  • 3
  • 7