datatables開啟服務(wù)器模式后,需要服務(wù)端做分頁搜索等對(duì)數(shù)據(jù)的處理擅耽。
/*
* @author Hiking活孩,Lin 2018/01/09
* @attention 本方法執(zhí)行mysql操作的環(huán)境是WordPress 其他框架請(qǐng)自行替換sql執(zhí)行方式(本框架執(zhí)行方法為get_row,get_results),具體詳見注釋乖仇!
* @attention 本方法不需要前端額外處理發(fā)送的數(shù)據(jù)結(jié)構(gòu)憾儒,服務(wù)端已經(jīng)處理。否則會(huì)找不到對(duì)應(yīng)數(shù)據(jù)乃沙!
* @attention 額外增加了時(shí)間的范圍搜索邏輯處理 需要時(shí)間搜索請(qǐng)前端自行傳值起趾!可以留空!
* @param $request datatable的數(shù)據(jù) startDate 代表開始時(shí)間 endDate 代表結(jié)束時(shí)間
* @param $tableName 操作的數(shù)據(jù)表
* @param $searchColumns 根據(jù)哪些列進(jìn)行模擬搜索警儒,一維數(shù)組训裆,可以為空數(shù)組
* @param $dataTime 根據(jù)哪個(gè)字段進(jìn)行時(shí)間范圍搜索 必須是Y-m-d H:i:s
*/
function class_demo_dt( $request,$tableName,$searchColumns,$dataTime ) {
// data table 需要四個(gè)參數(shù) // draw 全部的記錄數(shù) 過濾后的記錄數(shù) 數(shù)據(jù)
// 除了開始時(shí)間和結(jié)束時(shí)間需要前臺(tái)額外傳值 其他的值datatables自己會(huì)處理
$draw = $request['draw']; //會(huì)直接轉(zhuǎn)換int類型返回給前臺(tái)
$search = $request['search']['value']; //獲取前臺(tái)傳過來的過濾條件
$start = $request['start']; //從多少開始
$length = $request['length']; //數(shù)據(jù)長(zhǎng)度
$orderColumns = $request['order']['0']['column']; //那一列排序,從0開始
$orderDir = $request['order']['0']['dir']; //ase desc 升序或者降
$startDate = $request['startDate']; //開始時(shí)間
$endDate = $request['endDate']; //結(jié)束時(shí)間
// $orderDate = $request['columns'][$orderColumns]['data']; //獲取當(dāng)前需要排序的字段
$orderDate = 'update_date'; //獲取當(dāng)前需要排序的字段
// 如果有默認(rèn)排序的字段的話 拼接sql 沒有則為''
if( isset( $orderColumns ) ) {
$orderSql = " order by {$orderDate} ".$orderDir;
}else{
$orderSql = '';
}
// 分頁數(shù)據(jù) 控制每頁的顯示條數(shù)
$limitSql = '';
$limitFlag = isset( $start ) && $length != -1 ;
if ( $limitFlag ) {
$limitSql = " LIMIT ".intval($start).", ".intval($length);
}
//定義查詢數(shù)據(jù)總記錄數(shù)sql get_row是執(zhí)行查詢
$sumSql = "SELECT count(1) as sum FROM " .$tableName;
$recordsTotal = $this->wpdb->get_row( $sumSql, ARRAY_A )['sum'];
$totalresultSql = "SELECT * FROM " . $tableName;
//如果搜索條件(包括時(shí)間范圍搜索)不為空的話 返回什么值--
if ( strlen( $search ) > 0 || (strlen( $startDate ) > 0 && strlen( $endDate ) > 0)) {
// 兩個(gè)時(shí)間都有值的話 走這一步
if( !empty( $startDate ) || !empty($endDate ) ) {
if( count( $searchColumns ) > 0 ) {
$whereCondition = " WHERE ("; // 或者where
foreach ( $searchColumns as $keys => $value ) {
if ( $keys == 0 ){ // 如果單個(gè)匹配的話
$whereCondition .= $value . ' LIKE "%' . $search . '%" ';
}else { //如果是多個(gè)匹配的話
$whereCondition .= ' OR ' . $value . ' LIKE "%' . $search . '%" ';
}
}
$sql .= $whereCondition;
$timeSql = ') AND ' . " $dataTime BETWEEN '". "$startDate". "' AND '". "$endDate"."'";
}else{
// 零模擬搜索字段時(shí)
$whereCondition = " WHERE 1";
$sql .= $whereCondition;
$timeSql = ' AND ' . " $dataTime BETWEEN '". "$startDate". "' AND '". "$endDate"."'";
}
$sql .= $timeSql;
// return $sql;
}else{
$whereCondition = " AND ";
// $whereCondition = " WHERE ";
foreach ( $searchColumns as $keys => $value ) {
if ( $keys == 0 ){ // 如果單個(gè)匹配的話
$whereCondition .= $value . ' LIKE "%' . $search . '%" ';
}else { //如果是多個(gè)匹配的話
$whereCondition .= ' OR ' . $value . ' LIKE "%' . $search . '%" ';
}
}
$sql .= $whereCondition;
// return $sql;
}
// 獲取過濾后的數(shù)據(jù)總數(shù)量蜀铲!
$recordsFiltered = $this->wpdb->get_row( $sumSql.$sql.$orderSql.$limitSql, ARRAY_A )['sum'];
// return $totalresultSql.$sql.$orderSql.$limitSql;
// 獲取過濾后的數(shù)據(jù)边琉!
$infos = $this->wpdb->get_results( $totalresultSql.$sql.$orderSql.$limitSql,ARRAY_A );
// $infos = $this->wpdb->get_results( $totalresultSql.$sql.$orderSql.$limitSql,ARRAY_A );
}else {
$recordsFiltered = $recordsTotal;
// return $totalresultSql.$orderSql.$limitSql;
$infos = $this->wpdb->get_results( $totalresultSql.$orderSql.$limitSql,ARRAY_A );
}
return array(
"draw" =>intval($draw),
"recordsTotal" => intval($recordsTotal),
"recordsFiltered" => intval($recordsFiltered),
"data" => $infos
);
}
// 調(diào)用實(shí)例
function class_demo_test( $request ) {
return $this->class_demo_dt( $request,$tableName,$searchColumns,$dataTime );
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者