7

I spend so much time and can not udnerstand why datatables can not refresh my table, i am getting already crazy with this datatables

my code is below, i spent weeks on it but is impossible to get it to work and i use ajax.reload

DataTablesDraw = (selector, order, pages, file, sort, column, template, data_set) ->
$(selector).DataTable
  'pageLength': pages
  'ordering': sort
  'destroy' : true,
  'paging': true
  'responsive': true
  'searching': false
  'info': false
  'lengthChange': true
  'autoWidth': false
  'select': true
  'dom': 'Bfrtip',

  'buttons': [
          {
            'extend': 'excelHtml5',
            'title': file + new Date()
          },
          'copyHtml5'
      ],
  'order': [ [ column, 'desc' ] ],
  'language': {
        buttons: {
            copyTitle: i18n[lang]['id[9]'],
            copySuccess: {
                _: i18n[lang]['id[10]'] + ' %d ' + i18n[lang]['id[11]'],
                1: i18n[lang]['id[12]']
            }
        }
    }
  'ajax': '/settings/ranges/ranges.txt',
  "dataSrc": "data",
  'drawCallback': (settings) ->

    $('.dataTables_paginate > span').remove()
    excel = $('#DataTables_Table_' + order + '_wrapper .buttons-excel').detach()
    copy = $('#DataTables_Table_' + order + '_wrapper .buttons-copy').detach()

    if not $('#DataTables_Table_' + order + '_wrapper thead.tfoot').length
      $(this).append '<thead class="tfoot">' +
            '<tr>' +
                '<th colspan="10">' +
                    '<div class="export">' +
                        '<div class="buttons"></div>' +
                    '</div>' +
                    '<div class="paginator"></div>' +
                '</th>' +
            '</tr>' +
         '</thead>'

    paginator = $('#DataTables_Table_' + order + '_paginate').detach()
    $('#DataTables_Table_' + order + '_wrapper thead .paginator').append paginator
    $('#DataTables_Table_' + order + '_wrapper thead .export .buttons').append excel, copy

    if @fnPagingInfo().iTotalPages <= 1
      $('#DataTables_Table_' + order + '_paginate').hide()
      $('#DataTables_Table_' + order + '_info').hide()
    else
      $('#DataTables_Table_' + order + '_paginate').show()
      $('#DataTables_Table_' + order + '_info').show()

    return

  'columns': template
return

calling

table= DataTablesDraw '.__ranges__', 0, 25, 'Current Ranges ', true, 5, CurrentRangesTemplate, ranges #selector, order, pages, file name, sorting
table.ajax.reload()

the table is getting the data from the file everything is fine, just ajax.reload() cant fix to work

data

{"data": [{"status": "1", "environment": "demo", "currency": "EUR", "range_to": 42342, "date_update": 1491814286, "server": "server", "date_create": 1491814286, "platform": "platform", "range_from": 432423, "user": {"email": "[email protected]"}]}

2 Answers 2

16

The mistake was that u used ajax reload in this format

table.ajax.reload()

but in order to fix the mistake i jsut needed to do in this way

$('#table').DataTable().ajax.reload()

Hope this will help other, spent so much time on it on this small thing

Sign up to request clarification or add additional context in comments.

1 Comment

But why? The docs (datatables.net/reference/api/ajax.reload()) says its possible to do using the variable.
7

Please check your version of datatable Jquery for using ajax.reload(). If you are using older version like(1.10) then you have to use API of datatable.

$('#table_data').dataTable( ).api().ajax.reload();

It will be work for you.

or for reloading table, you can 1st destroy datatable and again load it.for eg.

 table = $("#table_data").datatable()
$("#my-button").click(function() {
    table.fnDestroy();
    table = $("#table_data").dataTable();
});

2 Comments

getting error: Uncaught TypeError: dataTable.fnDestroy is not a function.
Use DataTable() instead datatable()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.