thinkphp5配合datatable插件分页后端处理程序
第一版DataTable.php v.1.0
<?php
use think\Db;
/**
* DataTable.php.
*/
/**
* datatable结合thinkphp5使用的分页后台处理程序.
*
* @author B.I.T.
* @copyright Copyright (c) 2018-2019, B.I.T.
* @license
*
* @see 初始化
*
* @version v.1.0
*/
class DataTable
{
/**
* 数据库对象
*
* @var objectc
*/
public $_db;
/**
* 表名.
*
* @var string
*/
protected $_table;
/**
* 表格重绘次数.
*
* @var int
*/
protected $_draw;
/**
* 排序的是哪行.
*
* @var int
*/
protected $_order_column; //排序的行
/**
* 排序方式
* asc desc.
*
* @var string
*/
protected $_order_dir; //排序方式 asc desc
/**
* 查询的数据.
*
* @var string
*/
protected $_search = ''; //查询的字符串
/**
* 查询开始的位置.
*
* @var int
*/
protected $_start; //开始的位置
/**
* 每页显示的条目数.
*
* @var int
*/
protected $_length; //查询的长度
/**
* 过滤后的条目数量.
*
* @var integer
*/
protected $_recordsFiltered = 0; //过滤后的条目数量
/**
* 总的条目数量.
*
* @var integer
*/
protected $_recordsTotal = 0; //总的条目数量
/**
* 返回的数据 没用到.
*
* @var [type]
*/
protected $_return; //没用到
/**
* 存放构造函数的第二个参数.
*
* @var [type]
*/
protected $_info; //存放构造函数的第二个参数 /**
* 构造函数 处理datatables发送的数据,用户自定义条件,.
*
* @param array $dataTableGet dataTable前台传递过来的数组
* @param array $info 构造好的数组结构如下
* example array( //表示 select ID AS sum,ID,USERNMAE 其中 sum用来统计数据的总条数
* "select"=array(
* "ID"=>"sum",
* "0"=>'ID',
* "1"=>"USERNAME",
* ),
* "order"=>array( //前台会发送过来根据哪一列排序 接收过来的值就是 键值,对应到数据表中的字段就是值,前台有几个列能够排序这里就需要有几个对应的键值队
* "0"=>"ID",
* "2"=>"USERNAME",
* ),
* "where"=>array( //and和or可以同时调用 但是or是用来做查询的 and则是初始数据的查询条件
* "and"=>array( //表示会查询state=1 and level=2 and (a=1 or a=2) 的数据
* "state"=>'1',
* "level"=>"2",
* "a"=>[1,2]
* ),
*
* 注:"or" 用户搜索的时候存在一个问题,参与搜索查询的列必须为同一类型比如id和name字段就不可以一起搜索会出现输入
* "qwer" sql为 (id like 0 or name like %qwer%)的情况
*
* "or"=>array("ID","USERNAME"), //用户查询的时候回根据这里的参数作为查询的列 例如 当search=root时 就会查询 ID like %root% or USERNAME like %root%
* "or2"=>[ //这里面的会用or连接 几乎很少用 例如下面的数组会变成: a=1 or a=2 or b=2
* a=>[1,2],
* b=>[2]
* ]
* ),
* "join"=>array(
* 'class'=>array('student.class_id','class.id'),
* 'sex'=>array('student.class_id','sex.id')
* )
* )
* @param string $db 数据库对象
*
* @return object this
*/
public function __construct($dataTableGet, $info = array(), $table = '')
{
$this->_init($dataTableGet);
$this->_table = $table;
$this->_db = Db::table($this->_table);
$this->_info = $info;
} /**
* 初始化参数,检查数据格式.
*
* @param array $data datatables发送过来的数据
*/
protected function _init($data)
{
if (isset($data)) {
$this->_draw = isset($data['draw']) ? $data['draw'] : null;
$this->_length = isset($data['length']) ? intval($data['length']) : null;
$this->_start = isset($data['start']) ? intval($data['start']) : null;
$this->_order_column = isset($data['order']['0']['column']) ? intval($data['order']['0']['column']) : null;
$this->_order_dir = isset($data['order']['0']['dir']) ? $data['order']['0']['dir'] : null;
$this->_search = isset($data['search']['value']) ? $data['search']['value'] : null;
}
} /**
* 输出datatables需要的数据格式,还需要打成json的格式才行.
*
* @param bool $debug true 会输出一些调试信息 默认false
*
* @return array atatables需要的数据格式
*/
public function output($debug = false)
{
$data = $this->_info;
if (isset($data['select']) && !empty($data['select'])) {
$selectSql = $this->_getSelectSql($data['select']);
} else {
$selectSql = '*';
}
$this->_db = $this->_db->field($selectSql);
if (isset($data['join']) && !empty($data['join'])) {
foreach ($data['join'] as $key => $value) {
$this->_db = $this->_db->join($key,$value[0].' = '.$value[1]);
}
}
if (isset($data['order']) && !empty($data['order'])) {
$orderSql = $this->_getOrderSql($data['order']);
$this->_db = $this->_db->order($orderSql);
}
if (isset($data['where']) && !empty($data['where'])) {
$and = isset($data['where']['and']) && !empty($data['where']['and']) ? $data['where']['and'] : null;
$or = isset($data['where']['or']) && !empty($data['where']['or']) ? $data['where']['or'] : null;
$or2 = isset($data['where']['or2']) && !empty($data['where']['or2']) ? $data['where']['or2'] : null;
if(!is_null($and)){
foreach ($and as $key => $value) {
if(is_array($value)){
foreach ($value as $k => $v) {
$this->_db = $this->_db->whereOr($key,$v);
}
unset($and[$key]);
}
}
$this->_db = $this->_db->where($and);
}
if(!is_null($or)){
$or_val = '';
for ($i=0; $i < count($or); $i++) {
$or_val .= $i == count($or)-1 ? $or[$i] : $or[$i].'|';
}
if(!is_null($this->_search)&&$this->_search!=''){
$this->_db = $this->_db->where($or_val,'like','%'.$this->_search.'%');
}
}
if(!is_null($or2)){
foreach ($or2 as $key => $value) {
if(!is_array($value)) return false;
foreach ($value as $k => $v) {
$this->_db = $this->_db->whereOr($key,$v);
}
}
}
}
if (isset($this->_start) && isset($this->_length)) {
$this->_db = $this->_db->limit($this->_start,$this->_length);
}
if ($debug) {
var_dump($this->_db->fetchSql(true)->select());die;
}
$db1 = clone $this->_db;
$info = $this->_db->select();
$this->_recordsTotal = $db1->count();
$this->_recordsFiltered = $this->_recordsTotal;
return array(
'draw' => intval($this->_draw),
'recordsTotal' => intval($this->_recordsTotal),
'recordsFiltered' => intval($this->_recordsFiltered),
'data' => $info,
);
} /**
* 当前台需要排序的时候会根据需要排序的列对应的表中的值排序
* 构建排序的sql.
*
* @param array $data 构造函数第二个参数order部分
*
* @return string 排序部分的sql语句
*/
protected function _getOrderSql($data)
{
$sql = '';
if (is_array($data)) {
foreach ($data as $key => $value) {
if ($key == $this->_order_column) {
$sql = "{$value} ".$this->_order_dir;
break;
}
}
}
return $sql;
} /**
* 构建select部分的sql语句.
*
* @param array $data 构造函数第二个参数select部分
*
* @return string select部分的语句
*/
protected function _getSelectSql($data)
{
$list = array();
foreach ($data as $key => $value) {
if (is_numeric($key)) {
array_push($list, $value);
} else {
array_push($list, $key.' AS '.$value);
}
}
if (!empty($list)) {
$selectSql = implode(',', $list);
} else {
$selectSql = '*';
}
return $selectSql;
}
}
控制器类调用
<?php namespace app\index\controller; use think\Controller;
use think\Db;
use DataTable; class Index extends Controller
{
public function list()
{
return view('index/list');
} public function list_ajax()
{
$get = input('get.');
$where = [];
$data['select'] = ['think_user.id'=>'t_id','think_user.uname','think_user.upwd','think_banji.banji_name','think_user.status'];
$data['order'] = ['0'=>'think_user.id','1'=>'think_user.uname','2'=>'think_user.upwd','3'=>'think_banji.banji_name'];
// $data['where']['and'] = ['think_user.status'=>1,'think_user.id'=>[1,2]];
$data['where']['or'] = ['think_user.uname','think_user.upwd'];
$data['join'] = [
'think_banji'=>['think_user.banji_id','think_banji.id'],
];
$a = new DataTable($get, $data, 'think_user');
return $a->output();
}
}