简体   繁体   English

DataTables 服务器端分页

[英]DataTables server-side pagination

I have working Spring REST app with client side pagination, default by DataTables and everything works.我有使用客户端分页的 Spring REST 应用程序,默认为 DataTables,一切正常。 Now i need to change it to server-side pagination i have problem, because have no idea how to get information from DataTables what page number client want to see.现在我需要将其更改为服务器端分页我有问题,因为不知道如何从 DataTables 获取客户端想要查看的页码信息。 I can't find anything usefull in DT manual.我在 DT 手册中找不到任何有用的东西。

When you say Datatable I assume you are talking about DataTables jQuery plugin.当你说Datatable我假设你在谈论DataTables jQuery 插件。

To activate serverside pagination you need to pass要激活服务器端分页,您需要通过

"serverSide": true,

like this:像这样:

$('#example').DataTable( {
    "processing": true,
    "serverSide": true,
    "ajax": "/your_url"
});

Doing the above will activate your server-side pagination.执行上述操作将激活您的服务器端分页。 But you need to do few changes on the server-side too.但是您也需要在服务器端做一些更改。 Let's see them step by step.让我们一步一步地看看它们。

1. What happens when you mark serverSide as true 1. 当你将serverSide标记为true时会发生什么

DataTables plugin adds custom parameters to the AJAX call with information like DataTables 插件向 AJAX 调用添加自定义参数,其中包含诸如

order:  asc
start:  20
length: 10

// and many more.

You can inspect this demo link and see the parameters passed in request when you click the next page button.您可以检查此演示链接,并在单击下一页按钮时查看请求中传递的参数。

2. Similarly, DataTables plugin expects some fields in response in order to preserve pagination logic. 2. 同样,DataTables 插件需要一些字段作为响应以保留分页逻辑。

"draw": 3,             // unique ID
"recordsTotal": 57,    // total number of records
"recordsFiltered": 57  // total number of filtered records

You can inspect this demo link and see response data this time.这次您可以检查此演示链接并查看响应数据。

3. Now changes are on serverside in your API 3. 现在更改在您的 API 的服务器端

You need to add these parameters as queryParam for GET and attr in POST call in your controller API:您需要在控制器 API 的 POST 调用中为 GET 和 attr 添加这些参数作为queryParam

order: asc
start: 20
length: 10

4. Service Layer Changes - DB query 4.服务层变化-DB查询

In-Service Layer where you get details from a database.在服务层,您可以在其中从数据库获取详细信息。

You need to get the total number of records and in search query pass a LIMIT clause LIMIT 10, 10 in case of MySQL.您需要获取记录总数,并在搜索查询中传递 LIMIT 子句LIMIT 10, 10在 MySQL 的情况下。

Eg:例如:

SELECT * FROM User LIMIT 20,10;

Use start and length to calculate the next set of records.使用startlength计算下一组记录。

It can be trickier but if you understand and implement properly it's fun.这可能会更棘手,但如果您理解并正确实施,它就会很有趣。

Read more in detail here which also contains code and live demo.此处阅读更多详细信息其中还包含代码和现场演示。

Please see the sample about DataTables server-side processing: https://datatables.net/examples/server_side/simple.html请参阅DataTables服务器端处理示例: https : //datatables.net/examples/server_side/simple.html

After change page, you can capture the request to server as following format: https://.../server_processing.php ?更改页面后,您可以将请求捕获到服务器,格式如下: https://.../server_processing.php ? draw=3 &columns...& order=0&dir=asc&start=20&length=10 &search%5Bvalue%5D=&search%5Bregex%5D=false&_=1534436781912 draw=3 &columns...& order=0&dir=asc&start=20&length=10 &search%5Bvalue%5D=&search%5Bregex%5D=false&_=1534436781912

That means DataTable requests page number 3 (draw=3), order by first column ascending, ....这意味着 DataTable 请求第 3 页(draw=3),按第一列升序排序,....

At server side (REST), you can get page number by (for example) request.getParameter("draw")在服务器端(REST),您可以通过(例如) request.getParameter("draw") 获取页码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM