DataTables服务器端分页 [英] DataTables server-side pagination

查看:312
本文介绍了DataTables服务器端分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有带有客户端分页的Spring REST应用程序,DataTables默认使用它,并且一切正常。现在,我需要将其更改为服务器端分页,但是我有问题,因为不知道如何从DataTables中获取客户端想要查看的页码信息。我在DT手册中找不到任何有用的东西。

I have working Spring REST app with client side pagination, default by DataTables and everything works. 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. I can't find anything usefull in DT manual.

推荐答案

当您说 Datatable 我假定您正在谈论 DataTables jQuery插件。

When you say Datatable I assume you are talking about DataTables jQuery plugin.

要激活服务器端分页,您需要通过

To activate serverside pagination you need to pass

"serverSide": true,

像这样:

$('#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.

DataTables会发生什么插件将自定义参数添加到AJAX调用中,并提供以下信息,例如

DataTables plugin adds custom parameters to the AJAX call with information like

order:  asc
start:  20
length: 10

// and many more.

您可以检查文档链接,并在单击下一页按钮时查看在请求中传递的参数。

You can inspect this documentation link and see the parameters passed in request when you click the next page button.

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

您可以检查相同的链接并查看响应数据。

You can inspect same link and see response data this time.

您需要将这些参数添加为 queryParam ,以获取POST中的GET和attr在控制器API中调用:

You need to add these parameters as queryParam for GET and attr in POST call in your controller API:

order: asc
start: 20
length: 10



4。服务层更改-数据库查询



服务层,您可以从数据库中获取详细信息。

4. Service Layer Changes - DB query

In-Service Layer where you get details from a database.

您需要获取记录的总数,并在搜索查询中传递LIMIT子句 LIMIT 10,10

You need to get the total number of records and in search query pass a LIMIT clause LIMIT 10, 10 in case of MySQL.

例如:

SELECT * FROM User LIMIT 20,10;

使用 start length 来计算下一组记录。

Use start and length to calculate the next set of records.

这可能会比较棘手,但是如果您理解并正确实施,那就很有趣了。

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.

这篇关于DataTables服务器端分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆