用于大型数据集的Datatables.net [英] Datatables.net for large data sets

查看:85
本文介绍了用于大型数据集的Datatables.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 http://www.datatables.net/examples/data_sources/server_side.html 及其示例,但是很难遵循.在示例中,即使 https://datatables.net/manual/server-side 也是如此. /p>

在我走这条路之前,我无法回头,使用过Datatables的任何人都可以为我确认以下内容:

  1. 我最终可能会拥有一个拥有成千上万条记录的大型数据集;我可以做到吗,所以最初只加载了第一个记录,例如10条记录.然后,如果用户单击下一页"按钮,则显示记录11-20,如果再次单击下一页",则显示21-30,依此类推?每次(通过ajax/webservice)从数据库加载,因此对用户来说是快速的.

  2. 是否可以使搜索栏从数据库中搜索整个数据集?我可能需要过滤大约3列(例如用户名,电子邮件地址,邮政地址).

如果我能做这两件事,那么数据表就可以为我工作.因此,如果有经验的人可以就上述问题分享经验,那就太好了.谢谢.

解决方案

要同时回答1和2:是的,这正是服务器端功能的工作方式,非常容易实现.一个对象将发送到服务器端组件,其中包含有关搜索内容和返回多少信息(等于您一次在页面上指定的元素数)的信息,并且响应是一个仅包含足够数据的JSON对象在一页中.您在创建数据库时只需设置以下选项

"serverSide": true,
"ajax": "yourscript.php"

在此处查看他们提供的示例: http://www.datatables.net/examples/data_sources/server_side.html 拉起chrome inspector/firebug并查看网络"标签.您可以看到每次翻页和搜索时都来回发送有效负载(数据表会为您构造该有效负载,但是它使您有点了解它的工作原理).在同一页面上,您可以在选项卡中看到用于设置数据表客户端的javascript,然后是用于设置服务器端的php脚本.由于某种原因,第二个服务器端脚本似乎不在该页面上,但是您可以在这里找到它:

I'm reading http://www.datatables.net/examples/data_sources/server_side.html and their examples but this is seriously tough to follow. Even https://datatables.net/manual/server-side is light on the examples.

Before I go down this path so far that I can't turn back, can anyone who has used Datatables confirm the following for me:

  1. I may end up having a large dataset with thousands of records; can I make it so initially only the first, say, 10 records are loaded. And then if the user hits the "Next page" button, records 11-20 are shown, and if they hit "Next page" again, 21-30 are shown, and so on? Loaded from the database (through a ajax/webservice) each time so its quick for the user.

  2. Can I make it so the search bar searches through the entire dataset from the database? There's probably about 3 columns I'd need to be able to filter on (like username, email address, postal address).

If I can do both of these things, then Datatables can work for me. So if anyone who has experience with it can share their experience in regards to the above questions that would be great. Thanks.

解决方案

To answer both 1 and 2 at the same time: yes, this is exactly how the server side functionality works and it's pretty easy to implement. An object is sent to the server side component with information about what to search for and how many to return (equal to the number of elements you specify on a page at a time) and the response is a JSON object of only enough data to fit in one page. You simply set the following options when you create your datable

"serverSide": true,
"ajax": "yourscript.php"

Check out the sample they offer here: http://www.datatables.net/examples/data_sources/server_side.html Pull up chrome inspector/firebug and watch the network tab. You can see the payload being sent back and forth every time you page and search (datatables takes care of constructing this for you but it gives you a little bit of idea how exactly it works). On that same page you can see in the tabs the javascript that used to setup the datatable client side, then a php script thats used to setup the server side. For some reason the second server side script doesn't appear to be on that page but you can find it here: https://github.com/DataTables/DataTables/blob/master/examples/server_side/scripts/ssp.class.php

EDIT 1: to change the info text add this to your options:

language: {
        "info": "page _PAGE_ of _PAGES_"
    }

这篇关于用于大型数据集的Datatables.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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