У меня есть скрипт для загрузки данных в datatable, данные успешно загружены в таблицу, тогда я хочу попробовать обновить, когда datatable, если новые данные вставлены в базу данных, где отображается в json, я пытаюсь обновить datatable с автоматическим обновлением, просто обновите json из ajax . то я пытаюсь обновить datatable с помощью кнопки с обновлением идентификатора, но все еще не работает: есть ide?
<button id = "Refresh" type = "button">Refresh</button>
$(function dataQR () {
$('#showQR').on('show.bs.modal', function(e) {
$("#code").empty();
$("#code").qrcode($(e.relatedTarget).data('id'));
});
$.ajax({
url: "/report/dataGenerateQR.html",
dataType: "json",
method: "GET",
success: function (response) {
console.info("Response : "+ response);
var table = $('#QRTable').dataTable({
data: response,
retrieve: true,
paging: true,
searching: true,
//destroy: false,
columns: [
{'data': 'amount',"render": $.fn.dataTable.render.number(',', '.', 2, 'Rp. ')},
{'data': 'createdate'},
{
"data": "codeqr",
"render": function(data, type, row, meta){
if (type === 'display') {
data = '<a href = "#showQR" class = "fa fa-qrcode" data-toggle = "modal" data-target = "#showQR" data-id = "'+ data +'" style = "font-size: 1.6em;"></a>';
}
return data;
},
"orderable": false
}
]
});
//$('#QRTable').dataTable().api().ajax.reload();
$(".dataTables_filter input").addClass("form-control");
$(".dataTables_length select").addClass("form-control");
$("#Refresh").click(function(){
table.fnReloadAjax();
table.fnDraw();
})
},
error: function (jqXHR, textStatus, errorThrown) {
console.info("error bro :"+textStatus);
}
});
setTimeout(dataQR,2000);
});
какая-нибудь подсказка? Спасибо.

Попробуйте это перед fnDraw ():
table.dataTable().fnDestroy();
Вот мое решение. Надеюсь, это вам поможет :)
<script> var TableDatatablesAjax = function() {
var e = function() {
var a = new Datatable;
a.init({
src: $("#QRTable"),
onSuccess: function(a, e) {},
onError: function(a) {},
onDataLoad: function(a) {},
loadingMessage: "Loading...",
dataTable: {
lengthMenu: [
[10, 20, 50, 100, 150, -1],
[10, 20, 50, 100, 150, "All"]
],
"columnDefs": [ {
"targets": 'no-sort',
"orderable": false,
} ],
pageLength: 10,
processing: true,
serverSide: true,
bFilter:true,
//pagingType: "full_numbers",
ajax: {
url: "/report/dataGenerateQR.html",
data: function ( d ) {
d.customLabel='customValue';
// d.custom = $('#myInput').val();
// etc
}
},
//ordering: !1,
order: [
[1, "asc"]
]
}
})
};
return {
init: function() {
e()
}
} }();
jQuery(document).ready(function() {
TableDatatablesAjax.init();
//
$('#replace_with_button_id').click(function()
{
var QRTable = $('#QRTable').DataTable();
QRTable.ajax.reload();
});