使用AJAX进行数据表分页 [英] Datatables pagination with AJAX
问题描述
是否可以将数据表设置为分页,但仅加载从服务器显示的n个条目? (通过Ajax请求).
Is it possible to set datatables to paginate, but load only the n amount of entries that are displayed from the server? (via Ajax request).
换句话说,每次您重新排序或单击下n个条目"时,都会向服务器发出一个小的请求,以加载要显示的新条目.这样就可以最小化初始加载时间.
In other words, each time you re-sort or click 'Next n entries', a small request is made to the server to load the new entries to display. Thus minimizing the initial load time.
我将能够通过初始化来做到这一点,以便将其应用于我站点中的所有数据表吗?
Will I be able to do this via the initialization, so that it applies to all datatables in my site?
推荐答案
当然,这对于jquery-dataTables来说很酷.
Of course, this is the cool thing with jquery-dataTables.
看这个例子(针对PHP和MySQL).服务器端数据具有排序和分页
Look at this example (targeted at PHP and MySQL).Server sided Data with sorting and pagination
开箱即用.您要做的是在此PHP行中输入MySQL字段:
This works out of the box. A you have to do is to enter your MySQL fields into this PHP line:
$aColumns = array( 'engine', 'browser', 'platform', 'version', 'grade' );
如果您正在使用Node/mongoDB或任何其他服务器端脚本,则必须编写自己的代码. 但基本上:
If you are using Node/mongoDB or any other server sided script you will have to write your own code. But basically:
- 您在服务器端代码中设置了一个网址
- 您可以从请求中由dataTables设置的值(actual_Pagenumber,Items_per_page,按列排序,过滤)限制的任何数据源(db,json文件或其他)中获取数据.只需使用Firebug控制台查看典型请求即可.
- 您将json编码的数据返回给客户端(在
echo json_encode( $output );
中的PHP/MySql中.在node/mongo中,只是查询的结果.
- You set an url to your serversided code
- You fetch data from any datasource (db, json file or else) clamped by the values dataTables sets in the request(actual_Pagenumber, Items_per_page, sorting for columns, filtering). Just use the firebug console to look at a typical request.
- You return json-encoded Data to your client (In PHP/MySql by
echo json_encode( $output );
. In node/mongo just the result of your query.
这篇关于使用AJAX进行数据表分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!