我已经使用 Datatables 一段时间了,但显然,我没有以正确的方式使用它们。
过去,我会使用 $.post 开始我的数据表,如下所示:
$(document).ready(function() {
$.post('process/recordTables.php', function(data)
{
var table = $('#example1').DataTables();
table.clear();
var obj = JSON.parse(data);
obj.forEach(function(item)
{
table.row.add([item.column1, item.column2, //and so on]);
});
table.draw();
});
});
上面的代码有效。我的数据表打印我需要的数据。在 obj.forEach 中,如果需要,我可以设置带有 href 标签和其他功能的列。
但是,如果我希望能够按设定的时间间隔重新加载数据表,则不能使用上述方法。它必须使用“ajax”方法。
经过几次尝试和错误后,使用下面的代码,我能够打印出 jQuery 数据表的“正确”形式(和使用):
$(document).ready(function()
{
$('#example1').DataTable({
"processing": true,
"serverSide": true,
"ajax": {
"url": "process/recordTables.php", // same url as the above code
"type": "POST",
"dataSrc": ''
},
"columns": [
{ "data": "" },
{ "data": "column1" },
{ "data": "column2" },
{ "data": "column3" },
// and so on
],
"iDisplayLength": 25,
"order": [[ 3, "desc" ]],
"scrollY": 550,
"scrollX": true,
"bDestroy": true,
"stateSave": true,
// few others that don't seem to affect functionality
});
});
使用上面的代码,我能够使用 $.post 方法显示相同的数据。
但是...所有功能均无效。排序不起作用。分页、搜索过滤器、表格底部的数据信息显示“0 个条目中的 0 个”。什么都没用。
我缺少什么可以解决这个问题?我很困惑。
编辑
这是 HTML 表格:
<table id="example1" class="table table-bordered table-hover table-condensed">
<thead>
<tr>
<th></th>
<th>Column1</th>
<th>Column2</th>
<th>Column3</th>
// and so on
</tr>
</thead>
<tbody>
</tbody>
</table>
最佳答案
当您添加 "serverSide": true
属性时,您将 DataTable
设置为 server-side processing .在这种情况下,所有客户端处理都被禁用:
With server-side processing enabled, all paging, searching, ordering actions that DataTables performs are handed off to a server
关于javascript - jQuery 数据表功能无法操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45620577/