DataTables后台分页的使用

DataTables提供了完善的先后台分页功能,现将后台分页的学习和使用过程总结以下,方便往后参考。php

1、前台页面的配置

DataTables几乎能够在不改变前台代码部分便可实现前台分页到后台分页的转换,惟一须要作的就是在代码中开启DataTables后台分页功能便可:html

"serverSide" : true,// 服务器端分页处理

至此完成了前台页面的配置(就是这么简单,最主要的是后台处理逻辑的改变)git

2、后台处理逻辑的改变

要想使用后台分页,必须在后台使用服务器端语言处理过滤数据,而后将数据按照DataTables的要求返回到前台便可(具体要求见下文)github

DataTable提供了一个用来统一处理数据的类ssp.class.php,借用此类能够更加方便的实现后台逻辑部分正则表达式

3、DataTables参数信息

如下翻译仅供参考,若有错误请指正
开启后台分页后向后台发送的参数以及须要返回的数据要求以下:sql

  1. 发送的参数:
    当经过服务器端处理一个请求时,DataTables将发送以下数据给服务器端让其知道它所须要的数据json

参数名称 参数类型 参数说明
draw integer 请求序号。因为Ajax请求是异步的,和返回的参数draw一块儿用来肯定序号
start integer 当前从第几页开始(默认第一页为'0'
length integer 当前页所须要的数据条数(值为'-1'时表明返回全部的数据)
search[value] string 全局搜索的值(将应用在每个设置为可搜索的列中)
search[regex] boolean 全局搜索是否启用正则表达式
order[i][column] integer 排序将会应用到第i
order[i][dir] string 当前列的排序方向(asc=>正序,desc=>逆序)
columns[i][data] string 当前列数据源
columns[i][name] string 当前列名称
columns[i][searchable] boolean 当前列是否可搜索
columns[i][orderable] boolean 当前列是否可排序
columns[i][search][value] string 当前列搜索的值
columns[i][search][regex] boolean 当前列搜索是否启用正则表达式
  1. 须要返回的参数:
    DataTables须要以JSON的形式返回以下信息服务器

参数名称 参数类型 参数说明
draw integer 请求序号(DataTables强烈建议将此参数强制转换为int,以阻止可能的XSS攻击)
recordsTotal integer 过滤以前的总数据量
recordsFiltered integer 过滤以后的总数据量
data array 须要在表格中显示的数据
error string 错误信息,可选参数

4、ssp类使用示例

后台须要接收处理数据的文件server_processing.php,参考代码以下:异步

<?php
 
/*
* DataTables example server-side processing script.
*
* Please note that this script is intentionally extremely simply to show how
* server-side processing can be implemented, and probably shouldn't be used as
* the basis for a large complex system. It is suitable for simple use cases as
* for learning.
*
* See http://datatables.net/usage/server-side for full details on the server-
* side processing requirements of DataTables.
*
* @license MIT - http://datatables.net/license_mit
*/
 
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Easy set variables
*/
 
// DB table to use
$table = 'datatables_demo';
 
// Table's primary key
$primaryKey = 'id';
 
// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes
$columns = array(
    array( 'db' => 'first_name', 'dt' => 0 ),
    array( 'db' => 'last_name',  'dt' => 1 ),
    array( 'db' => 'position',   'dt' => 2 ),
    array( 'db' => 'office',     'dt' => 3 ),
    array(
        'db'        => 'start_date',
        'dt'        => 4,
        'formatter' => function( $d, $row ) {
            return date( 'jS M y', strtotime($d));
        }
    ),
    array(
        'db'        => 'salary',
        'dt'        => 5,
        'formatter' => function( $d, $row ) {
            return '$'.number_format($d);
        }
    )
);
 
// SQL server connection information
$sql_details = array(
    'user' => '',
    'pass' => '',
    'db'   => '',
    'host' => ''
);
 
 
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* If you just want to use the basic configuration for DataTables with PHP
* server-side, there is no need to edit below this line.
*/
 
require( 'ssp.class.php' );
 
echo json_encode(
    SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);

5、Tips:

  1. 因为ssp类的限制(后期可本身改写,解除限制),没法进行多表查询,可经过建立视图的折中方式解决问题;ide

  2. 可使用ssp类中的complex方法来实现对数据过滤更加高级的处理;

参考文档:

  1. 官方Manual: Server-side processing;

  2. 官方examples: Server-side processing;

  3. DataTablesGitHub地址.

关于我

文章转载自个人博客:
Heier Blog:Heier Home

相关文章
相关标签/搜索