这将纯粹关注“服务器端”解决方案的数据表方面。如何编写支持它所需的服务器端逻辑超出了本答案的范围。但我希望这些注释至少能够澄清该逻辑需要是什么,以及如何实现它。
假设
假设您有一个文本文件,其中包含 1,000 行这样的数据(或一百万行 - 但太多行,无法一次发送到浏览器和 DataTables)。该文本文件是一个简单的管道分隔文件,具有三个字段:
id|name|description
1|widget_1|This is a description for widget 1
2|widget_2|This is a description for widget 2
3|widget_3|This is a description for widget 3
...
1000|widget_1000|This is a description for widget 1000
您希望使用服务器端处理一次向 DataTables 发送 10 个项目。
您的数据映射到一个简单的 JSON 结构,如下所示 - 一组对象(每个对象是一个记录):
[
{
"id": 1,
"name": "widget_1",
"description": "This is a description for widget 1"
},
{
"id": 2,
"name": "widget_2",
"description": "This is a description for widget 2"
},
... // more records...
]
数据表定义
您的数据表定义如下所示 - 在此阶段,它故意非常简单:
<body>
<div style="margin: 20px;">
<table id="demo" class="display dataTable cell-border" style="width:100%">
</table>
</div>
<script type="text/javascript">
$(document).ready(function() {
$('#demo').DataTable({
serverSide: true,
ajax: {
url: 'http://localhost:7000/data',
type: 'POST'
},
columns: [
{ title: 'ID',
data: 'id' },
{ title: 'Name',
data: 'name' },
{ title: 'Description',
data: 'description' }
]
});
});
</script>
</body>
初步反应
当网页首次显示时,它将向 URL (http://localhost:7000/数据 http://localhost:7000/data),并且它将期望从 Web 服务器接收 JSON 响应,其中包含要显示的数据。
因为 DataTables 正在使用serverSide: true
,DataTables 期望 JSON 具有特定的结构,如所述here https://datatables.net/manual/server-side#Returned-data.
具体来说,服务器必须添加所有必填字段(draw
, recordsTotal
, recordsFiltered
, and data
) 到它发送到 DataTables 的 JSON。
在我们的例子中,它看起来像这样 - 请注意,它只是我们之前提到的 JSON 结构,添加了一些额外的元数据字段:
{
"draw": 1,
"recordsTotal": 1000,
"recordsFiltered": 1000,
"data": [{
"id": 1,
"name": "widget_1",
"description": "This is a description for widget 1"
}, {
"id": 2,
"name": "widget_2",
"description": "This is a description for widget 2"
}, {
"id": 3,
"name": "widget_3",
"description": "This is a description for widget 3"
}, {
"id": 4,
"name": "widget_4",
"description": "This is a description for widget 4"
}, {
"id": 5,
"name": "widget_5",
"description": "This is a description for widget 5"
}, {
"id": 6,
"name": "widget_6",
"description": "This is a description for widget 6"
}, {
"id": 7,
"name": "widget_7",
"description": "This is a description for widget 7"
}, {
"id": 8,
"name": "widget_8",
"description": "This is a description for widget 8"
}, {
"id": 9,
"name": "widget_9",
"description": "This is a description for widget 9"
}, {
"id": 10,
"name": "widget_10",
"description": "This is a description for widget 10"
}]
}
服务器负责构建此 JSON - 服务器数据集的前 10 条记录。服务器还告诉 DataTables 它总共有 1,000 条记录,并且它尚未过滤掉任何数据 - 因此过滤后总共也有 1,000 条记录。
DataTables 需要所有这些信息,因此它知道要显示多少个分页按钮以及要显示哪些分页数据。
请注意,完成所有这些工作完全是服务器的责任 - 这就是为什么它被称为“服务器端”处理。
客户端(浏览器)只有 10 条记录需要渲染 - 所以速度很快。
(我刚刚注意到屏幕截图提到“500 条记录”——这是我的服务器端代码中的一个错误——没有过滤器,所以我需要修复它)。
后续请求
当用户单击页面导航按钮(例如页面“4”)时,会触发从 DataTables 到服务器的新请求。 DataTables 使用描述的字段自动构建此请求here https://datatables.net/manual/server-side#Sent-parameters.
该请求作为表单数据发送。
在我们的示例中,请求如下所示:
"Form data": {
"draw": "5",
"columns[0][data]": "id",
"columns[0][name]": "",
"columns[0][searchable]": "true",
"columns[0][orderable]": "true",
"columns[0][search][value]": "",
"columns[0][search][regex]": "false",
"columns[1][data]": "name",
"columns[1][name]": "",
"columns[1][searchable]": "true",
"columns[1][orderable]": "true",
"columns[1][search][value]": "",
"columns[1][search][regex]": "false",
"columns[2][data]": "description",
"columns[2][name]": "",
"columns[2][searchable]": "true",
"columns[2][orderable]": "true",
"columns[2][search][value]": "",
"columns[2][search][regex]": "false",
"order[0][column]": "1",
"order[0][dir]": "asc",
"start": "30",
"length": "10",
"search[value]": "",
"search[regex]": "false"
}
这些字段告诉服务器它需要知道的一切,以便它可以准备正确的响应。
在我们的例子中,最重要的字段是:
"start": "30",
"length": "10"
从第 30 行开始,提供 10 条记录。
同样,服务器有责任准备准确反映所请求数据的 JSON 响应。
在我们的例子中,这意味着服务器需要具有逻辑来读取文本文件到正确的起点(数据行 31 - 记住偏移量从零开始),总共 10 行(第 31 行到第 40 行)。
上述来自 DataTables 的请求中的其他字段描述了如何对数据进行排序和过滤。在我们的例子中没有过滤器"search[value]": "",
- 数据将按第一列升序排序。
最后的注释
我故意不描述以下内容:
1) 服务器端代码如何处理发送回 DataTables 的 JSON 响应的创建;
2) 服务器端代码如何解析从 DataTables 收到的表单请求。
这完全取决于您的服务器端技术是什么。数据表不在乎。它只是传递 JSON 消息 - 它与服务器端实现分离 - 正如它应该的那样。
关于描述的“延迟渲染”选项here https://datatables.net/reference/option/deferRender,如果您觉得需要,您可以选择添加该增强功能。但我建议首先让一个更基本的服务器端实现工作起来。