2
有沒有一種方法可以自動刷新jQuery數據表中的數據?我想單擊一個按鈕,該按鈕將發出一個AJAX請求以返回新數據並將數據插入到數據表中。我想是這樣的:使用Ajax動態刷新jQuery數據表
$("#update_button").click(function(e) {
$.ajax({
type: "POST",
data: productType,
url: "/api/getproducts",
success: function(data, status) {
$('#products_table').DataTable({
data: data,
columns: [
{ title: "col 1" },
{ title: "col 2" },
{ title: "col 3" },
{ title: "col 4" },
{ title: "col 5" },
{ title: "col 6" }
]
});
},
error: function(data, status, res) {
console.log('error...');
console.log(res);
}
});
});
但是這給了我「表不能重新初始化」的錯誤...有人能幫忙嗎?
謝謝!
見'https://datatables.net/reference/api/ajax.reload()','ajax.reload()'是一個可用庫重新加載的jQuery數據表從他們的ajax源。 – stark
檢查此問題http://stackoverflow.com/questions/32049439/how-to-set-dynamically-the-ajax-url-of-a-datatable/32049573#32049573 – Bugfixer
您應該從初始化代碼中取出初始化代碼事件,只需調用'draw()'來刷新數據。 – markpsmith