what's wrong with my dataTable ajax reload

what's wrong with my dataTable ajax reload

mending3mending3 Posts: 16Questions: 9Answers: 0
edited August 2017 in Free community support

is there anything wrong with my ajax.reload after ajax (sweetalert) success callback?

var table = $('#dataTable').dataTable({
"language": {
"lengthMenu": "Munculkan MENU Data",
"search": "Cari Berdasarkan Nama:",
"info": "Menghasilkan Data Sebanyak PAGE Dari PAGES Tabel"
}
});

success: function (response) {
swal('Deleted!', response.message, response.status);
table.ajax.reload(null, false);
},

Been trying with table.api()... , shows me: 'DataTables warning: table id=dataTable blabla' and table.ajax.reload(); still shows me 'TypeError: table.ajax is undefined' in the Console

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,174Questions: 26Answers: 4,923

    I'm assuming you are using a recent version of Datatables?

    If so then this:

    var table = $('#dataTable').dataTable({

    Should look like this:

    var table = $('#dataTable').DataTable({

    Notice the capital d for DataTable. See the first FAQ here for details:
    https://datatables.net/faqs/index#Most-common-FAQs

    I believe for ajax.reload() to work you need to define the ajax using the Datatable's ajax. If you define outside of DT then it doesn't have the URL defined to execute the ajax.reload().

    If you want to use ajax outside of the Datatables init options then you can use clear() followed by rows.add() to populate the table with the new data.

    Kevin

  • mending3mending3 Posts: 16Questions: 9Answers: 0

    Thank you for your response. I'm using the ajax outside of DT, cause it contains swal for confirmation. However, can the swal be inside the DT? Anyway, gonna give a shot for that method. Thank you again

  • allanallan Posts: 63,214Questions: 1Answers: 10,415 Site admin
    Answer ✓

    If you aren't using the ajax option ajax.reload() isn't going to help you. You'd need to use clear() to clear the table and then rows.add() to add new rows that you've got from your external Ajax.

    Allan

This discussion has been closed.