Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataTables ajax.reload() not working

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]"}]}
like image 552
aaa Avatar asked Apr 10 '17 10:04

aaa


2 Answers

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

like image 199
aaa Avatar answered Nov 11 '22 11:11

aaa


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();
});
like image 32
Priya-Systematix Avatar answered Nov 11 '22 12:11

Priya-Systematix