Вот процесс, который я использую: Сводка:
Объявление переменной js в области верхнего уровня
В документе .ready, создайте экземпляр таблицы данных в вашей глобальной js переменной
On ajax после очистки таблицы с помощью сохраненного объекта, а затем итерации результатов и добавления строк
Более подробно:
//declare this high in scope so you can access it in your functions
var dt;
$(document).ready(function () {
//Create the datatable and assign it to variable for later reference
dt = $('#MyTable').DataTable({dom: 'Bfrtip'});
});
$("#SomeBtn").click(function () {
//clear current table rows
dt.clear().draw();
$.ajax({
url: whatever Path,
data: whatever Data,
dataType: "json",
type: "POST",
cache: false,
contentType: "application/json; charset=utf-8",
success: function (data) {
$.each(data, function (i, n) {
//Iterate results and add each to the table. This is why we stored the datatable in a highr scope so we can operate it on it here :)
dt.row.add([n.prop1, n.prop2, n.prop3]).node().id = n.propID + '_Row';
dt.draw(false);
});
},
error: function (response) {
},
failure: function (response) {
}
});
});
Как вы можете видеть, сохраняя данные в объекте при его создании, вы можете управлять им в последующих функциях в соответствии с его API.