数据表中的自定义分页 [英] Custom Pagination in datatable

查看:23
本文介绍了数据表中的自定义分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Web 应用程序,我在其中从数据库中获取数据并显示在数据表中.我在执行此操作时遇到问题,因为我正在获取的数据行数过多(200 000).所以当我查询类似 select * from table_name;我的应用程序卡住了.

I have a web application in which I get data from my database and show in a datatable. I am facing an issue doing this as the data that I am fetching has too many rows(200 000). So when I query something like select * from table_name; my application gets stuck.

有没有办法用 JavaScript 处理这个问题?

Is there a way to handle this problem with JavaScript?

我尝试了分页,但我不知道该怎么做,因为数据表会为已经呈现的数据创建分页?

I tried pagination but I cannot figure how would i do that as datatable creates pagination for already rendered data?

有没有办法通过分页运行我的查询后台?

Is there a way through which I can run my query through pagination at the backend?

推荐答案

我在使用 mongodb 和 angularjs 时遇到了同样的问题.我使用了服务器端分页.由于您有大量记录,您可以尝试使用相同的方法.

I have come across the same problem when working with mongodb and angularjs. I used server side paging. Since you have huge number of records, You can try using the same approach.

假设您在一页中显示 25 条记录.

Assuming a case that you are displaying 25 records in one page.

后端:

  1. 使用 COUNT 查询获取记录总数.
  2. select * from table_name LIMIT 25 OFFSET${req.query.pageNumber*25} 根据页码查询有限的记录;
  1. Get the total count of the records using COUNT query.
  2. select * from table_name LIMIT 25 OFFSET ${req.query.pageNumber*25} to query limited records based on the page number;

前端:

  1. 不要使用数据表,而是自行在 HTML 表中显示数据.
  2. 定义下一页和上一页的按钮.
  3. 在controller/js 文件中为pageNumber 定义全局变量.单击下一页按钮时将 pageNumber 增加 1 并且按下 prev 按钮时减 1.
  4. 使用 COUNT 查询的结果设置 pageNumber 的上限变量.(如果有 200 条记录,则限制为 200/25=8).

所以基本上 select * from table_name LIMIT 25 OFFSET${req.query.pageNumber*25} 将记录数限制为 25.当 req.query.pageNumber=1 时,它将偏移前 25 条记录并发送接下来的 25 条记录.同理,如果req.query.pageNumber=2,则偏移前2*25条记录,发送51-75条记录.

So basically select * from table_name LIMIT 25 OFFSET ${req.query.pageNumber*25} will limit the number of records to 25. when req.query.pageNumber=1, it will offset first 25records and sends next 25 records. similarly if req.query.pageNumber=2, it will offset first 2*25 records and sends 51-75 records.

这篇关于数据表中的自定义分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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