您可以通过初始(单独的)Ajax 调用提供 deferLoading 值 - 例如,使用 jQuery ajax。然后,您可以在完成初始 Ajax 调用后将该值传递给 DataTable。
所以,假设我们已经构建了 HTML 表格并在其中填充了一些数据:
<table id="example" class="display dataTable cell-border" style="width:100%">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Position</th>
<th>Salary</th>
<th>Start date</th>
<th>Office</th>
<th>Extn.</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Tiger Nixon</td>
<td>System Architect</td>
<td>$320,800</td>
<td>2011/04/25</td>
<td>Edinburgh</td>
<td>5421</td>
</tr>
<tr> ... and more initial rows... </tr>
</tbody>
</table>
那么我们可以使用如下脚本:
$(document).ready(function() {
$.ajax({
url: "YOUR_URL_HERE/preload-count"
})
.done(function( data ) {
initializeTable( data.count );
});
function initializeTable(deferDataCount) {
$('#example').DataTable( {
serverSide: true,
"deferLoading": deferDataCount,
ajax: {
url: "YOUR_URL_HERE/serverside-data",
},
"columns": [
{ "title": "ID", "data": "id" },
{ "title": "Name", "data": "name" },
{ "title": "Position", "data": "position" },
{ "title": "Salary", "data": "salary" },
{ "title": "Start Date", "data": "start_date" },
{ "title": "Office", "data": "office" },
{ "title": "Extn.", "data": "extn" }
]
});
}
} );
在上面的脚本中,我们首先调用如下URL:
url: "YOUR_URL_HERE/preload-count"
这将返回一个简单的 JSON 结构,其中包含我们要在 deferLoading 选项中使用的计数 - 所以,在我的例子中是这样的:
{
"count": 57
}
然后将该整数传递给我的initializeTable(deferDataCount) 函数,在该函数中使用我需要使用的计数来初始化 DataTable:
"deferLoading": deferDataCount,
现在,下次执行用户操作(分页/过滤/排序)时,DataTable 将使用其 URL 来获取下一页数据:
url: "YOUR_URL_HERE/serverside-data"
此 URL 将返回 DataTables 所需的所有 serverSide 值,包括行数据 JSON(在我的情况下)如下所示:
{
"id": "2",
"name": "Garrett Winters",
"position": "Accountant",
"salary": "$170,750",
"start_date": "2011/07/25",
"office": "Tokyo",
"extn": "8422"
}
更新
这是我的完整网页,供参考,以展示所有部分如何组合在一起:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Demo</title>
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script src="https://cdn.datatables.net/1.10.23/js/jquery.dataTables.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.23/css/jquery.dataTables.css">
<link rel="stylesheet" type="text/css" href="https://datatables.net/media/css/site-examples.css">
</head>
<body>
<div style="margin: 20px;">
<table id="example" class="display dataTable cell-border" style="width:100%">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Position</th>
<th>Salary</th>
<th>Start date</th>
<th>Office</th>
<th>Extn.</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Tiger Nixon</td>
<td>System Architect</td>
<td>$320,800</td>
<td>2011/04/25</td>
<td>Edinburgh</td>
<td>5421</td>
</tr>
</tbody>
</table>
</div>
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
url: "YOUR_URL_HERE/preload-count"
})
.done(function( data ) {
initializeTable( data.count );
});
function initializeTable(deferDataCount) {
$('#example').DataTable( {
serverSide: true,
"deferLoading": deferDataCount,
ajax: {
url: "YOUR_URL_HERE/serverside-data",
},
"columns": [
{ "title": "ID", "data": "id" },
{ "title": "Name", "data": "name" },
{ "title": "Position", "data": "position" },
{ "title": "Salary", "data": "salary" },
{ "title": "Start Date", "data": "start_date" },
{ "title": "Office", "data": "office" },
{ "title": "Extn.", "data": "extn" }
]
});
}
} );
</script>
</body>
</html>