Javascript 使用AJAX和Datatables刷新Datatable
我是web开发新手,实际上我正在使用Codeigniter和Bootstrap开发一些仪表板。 我有一个问题,我需要输出一个包含大量数据的datatable,所以我想使用Datatables。 问题是,当第一次加载页面时,一切都正常,但是在AJAX刷新之后,我丢失了数据表中的分页和JavaScript 现在,我正在使用以下代码创建表:Javascript 使用AJAX和Datatables刷新Datatable,javascript,jquery,datatables,Javascript,Jquery,Datatables,我是web开发新手,实际上我正在使用Codeigniter和Bootstrap开发一些仪表板。 我有一个问题,我需要输出一个包含大量数据的datatable,所以我想使用Datatables。 问题是,当第一次加载页面时,一切都正常,但是在AJAX刷新之后,我丢失了数据表中的分页和JavaScript 现在,我正在使用以下代码创建表: function refreshTabellaApple() { $('#tabellaApple').empty(); $.ajax({
function refreshTabellaApple() {
$('#tabellaApple').empty();
$.ajax({
method: "POST",
url: base_url + 'dashboard/ajax_tabella_apple',
success: function(data) {
var tbody = $("<tbody />"),
thead = $("<thead><tr><th class='m-0 font-weight-bold text-primary h5'>Data Recensione</th><th class='m-0 font-weight-bold text-primary h5 text-center'>Utente</th><th class='m-0 font-weight-bold text-primary h5 text-center'>Titolo</th><th class='m-0 font-weight-bold text-primary h5 text-center'>Recensione</th><th class='m-0 font-weight-bold text-primary h5 text-center'>Risposta</th></tr></thead>"),
tr;
$.each(JSON.parse(data), function(_, obj) {
tr = $("<tr />");
tr
.append("<td class='m-0'>" + obj.dt_datarecensione + "</td>")
.append("<td class='m-0'>" + obj.utente + "</td>")
.append("<td class='m-0'>" + obj.titolo + "</td>")
.append("<td class='m-0'>" + obj.recensione + "</td>")
.append("<td class='m-0'>" + obj.risposta + "</td>")
tr.appendTo(tbody);
});
$('#tabellaApple').append(thead);
$('#tabellaApple').append(tbody);
setTimeout(function() {
refreshTabellaApple(); //this will send request again and again;
}, 30000);
}
});
};
refreshTabellaApple();
函数refreshttabellaapple(){
$('tabellaappe').empty();
$.ajax({
方法:“张贴”,
url:base_url+“dashboard/ajax_tabella_apple”,
成功:功能(数据){
变量tbody=$(“”),
thead=$(“数据接收EUTENTETOTOLERIERSPOSTA”),
tr;
$.each(JSON.parse(数据),函数(x,obj){
tr=$(“”);
tr
.append(“+obj.dt_dataRecension+”)
.append(“+obj.utenet+”)
.append(“+obj.titolo+”)
.append(“+obj.recension+”)
.append(“+obj.risposta+”)
tr.appendTo(tbody);
});
$('tabellaappe')。追加(thead);
$('#tabellaappe')。追加(tbody);
setTimeout(函数(){
refreshTabellaApple();//这将一次又一次地发送请求;
}, 30000);
}
});
};
苹果;
如您所见,我正在JavaScript中构建表体,然后每30秒刷新一次。
在另一个js文件中,我将该表称为Datatable
<script type="text/javascript">
$(document).ready(function() {
$('#tabellaApple').DataTable();
});
</script>
$(文档).ready(函数(){
$('#tabellaappe').DataTable();
});
如何使用Datatables重建函数refreshTabellaApple?一种方法是使用Datatables内置的对
ajax
的支持,而不是使用jQueryajax
调用。然后,还可以使用DataTablereload()
选项。DataTables网站有一个基本功能。