ThinkPHP整合datatables實(shí)現(xiàn)服務(wù)端分頁(yè)的示例代碼
最近做東西有一個(gè)需求,因?yàn)閿?shù)據(jù)量很大,在這里我決定使用datatables的服務(wù)端分頁(yè),同時(shí)還需要傳遞查詢條件到服務(wù)端。在網(wǎng)上搜索的大部分文章都感覺(jué)有些誤差,于是自己封裝了一下,主要配置/工具為:
服務(wù)端:php(使用thinkphp)
頁(yè)面樣式來(lái)自于H-ui框架(datatables版本為1.10.0)
主要修改(databases)配置項(xiàng)為:
1) bProcessing:true 使用ajax源
2) serverSide:true 使用服務(wù)端分頁(yè)
3) createdRow:function(){} 回調(diào)函數(shù),用于添加事件或類名
4) aoColumns 用于處理、顯示數(shù)據(jù),其中render屬性用于自定義列
1.datatables 的js代碼為:
$('.table-sort').dataTable({
processing: true,
serverSide: true,
ajax: {
"url":"{:U('Msg/index')}",
"data":function(d){ //額外傳遞的參數(shù)
d.mintime = $('#logmin').val();
d.maxtime = $('#logmax').val();
}
},
bStateSave: true,//狀態(tài)保存
aLengthMenu : [20, 30, 50, 100, 150],
bProcessing : true,
bAutoWidth: false,
bFilter : true, //是否啟動(dòng)過(guò)濾、搜索功能
bInfo : true, //是否顯示頁(yè)腳信息,DataTables插件左下角顯示記錄數(shù)
createdRow: function ( row, data, index ) {
$(row).addClass('text-c');
$('#count').html(data.recordsFiltered);
},
aoColumns: [
{
"sClass": "text-center",
"data": "id",
"render": function (data, type, full, meta) {
return '<input type="checkbox" name="select" value="' + data + '" />';
},
"bSortable": false
},
{ "mData": "id" },
{ "mData": "fromnickname"},
{ "mData": "content" },
{ "mData": "msgtype" },
{ "mData": "time"},
{
"sClass": "text-center",
"data": "id",
"render": function (data, type, full, meta) {
html = '<a title="查看" href="javascript:;" rel="external nofollow" rel="external nofollow" onclick="show(`查看`,`__URL__/show/id/'+ data + '`,``,`610`)" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">查看</i></a>';
html += '<a style="text-decoration:none" class="ml-5" onClick="signDel(this,'+ data +')" href="javascript:;" rel="external nofollow" rel="external nofollow" title="刪除"><i class="Hui-iconfont"></i>刪除</a>';
return html;
},
"bSortable": false
}
]
});
2.服務(wù)端方面:
控制器: 接收參數(shù)如下: draw 前端傳過(guò)來(lái)的值,原值返回,用于驗(yàn)證 mintime、maxtime 自定義參數(shù)(時(shí)間) search.value datatables搜索框參數(shù),用于查詢篩選 order.0.column 要排序的單元格(從0開(kāi)始,字段需要自己設(shè)置) order.0.dir 排序(升序、降序) start 起始條數(shù)(第幾條開(kāi)始) length 查詢長(zhǎng)度 返回的數(shù)據(jù)如下: draw 返回前端傳過(guò)來(lái)的值 recordsTotal 記錄總條數(shù) recordsFiltered 條件篩選后的記錄總條數(shù) data 服務(wù)端查詢的數(shù)據(jù) 返回?cái)?shù)據(jù)形式:json
3.服務(wù)端后端完整代碼如下:
1)控制器代碼:
public function index()
{
if(IS_AJAX){
$list = D('Msg')->getData(I('get.'));
$this->ajaxReturn($list);
}
$this->display();
}
2) Model層代碼:(*其中,dealTime方法主要用于處理時(shí)間段)
public function getData($data)
{
//獲取Datatables發(fā)送的參數(shù) 必要
$draw = $data['draw']; //這個(gè)值直接返回給前臺(tái)
//獲取時(shí)間區(qū)間
$timeArr['mintime'] = $data['mintime'];
$timeArr['maxtime'] = $data['maxtime'];
$where = $this->dealTime($timeArr);
//搜索框
$search = trim($data['search']['value']); //獲取前臺(tái)傳過(guò)來(lái)的過(guò)濾條件
if(strlen($search) > 0) {
$where['id|fromnickname|content|msgtype'] = array('like','%'.$search.'%');
}
//定義查詢數(shù)據(jù)總記錄數(shù)sql
$recordsTotal = $this->count();
//定義過(guò)濾條件查詢過(guò)濾后的記錄數(shù)sql
$recordsFiltered = $this->where($where)->count();
//排序條件
$orderArr = [1=>'id', 2=>'fromnickname', 3=>'content', 4=>'msgtype', 5=>'time'];
//獲取要排序的字段
$orderField = (empty($orderArr[$data['order']['0']['column']])) ? 'id' : $orderArr[$data['order']['0']['column']];
//需要空格,防止字符串連接在一塊
$order = $orderField.' '.$data['order']['0']['dir'];
//按條件過(guò)濾找出記錄
$result = [];
//備注:$data['start']起始條數(shù) $data['length']查詢長(zhǎng)度
$result = $this->field('id,fromnickname,content,msgtype,time')
->where($where)
->order($order)
->limit(intval($data['start']), intval($data['length']))
->select();
//處理數(shù)據(jù)
if(!empty($result)) {
foreach ($result as $key => $value) {
$result[$key]['time'] = date("Y-m-d H:i:s",$value['time']);
$result[$key]['recordsFiltered'] = $recordsFiltered;
}
}
//拼接要返回的數(shù)據(jù)
$list = array(
"draw" => intval($draw),
"recordsTotal" => intval($recordsTotal),
"recordsFiltered"=>intval($recordsFiltered),
"data" => $result,
);
return $list;
}
3) 實(shí)現(xiàn)自定義ajax搜索
1. 在WdatePicker中添加onpicked回調(diào)函數(shù) 2. 執(zhí)行table.fnFilter(),其中table為datatables對(duì)象
以WdatePicker插件為例(input框類似,綁定onchange事件即可):
<input type="text" onfocus="WdatePicker({maxDate:'#F{ $dp.$D(\'logmax\')||\'%y-%M-%d\'}', onpicked:function(){table.fnFilter();}})" name="mintime" id="logmin" class="input-text Wdate" style="width:120px;">
3. datatables中ajax屬性中data屬性定義額外要傳遞的參數(shù)
例子:
ajax: {
"url":"{:U('Msg/index')}",
"data":function(d){ //額外傳遞的參數(shù)
d.mintime = $('#logmin').val();
d.maxtime = $('#logmax').val();
}
4) 代碼截圖:
a. html頁(yè)面
b.js部分
以上這篇ThinkPHP整合datatables實(shí)現(xiàn)服務(wù)端分頁(yè)的示例代碼就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Thinkphp3.2.3整合phpqrcode生成帶logo的二維碼
- ThinkPHP整合百度Ueditor圖文教程
- Thinkphp整合微信支付功能
- thinkphp整合微信支付代碼分享
- thinkPHP5框架整合plupload實(shí)現(xiàn)圖片批量上傳功能的方法
- ThinkPHP 整合Bootstrap Ajax分頁(yè)樣式
- ThinkPHP上使用多說(shuō)評(píng)論插件的方法
- ThinkPHP3.2.2的插件控制器功能簡(jiǎn)述
- 基于ThinkPHP5.0實(shí)現(xiàn)圖片上傳插件
- Thinkphp和onethink實(shí)現(xiàn)微信支付插件
- ThinkPHP使用Smarty第三方插件方法小結(jié)
- thinkPHP框架整合tcpdf插件操作示例
相關(guān)文章
Yii2框架配置文件(Application屬性)與調(diào)試技巧實(shí)例分析
這篇文章主要介紹了Yii2框架配置文件(Application屬性)與調(diào)試技巧,結(jié)合實(shí)例形式分析了Yii框架配置文件使用方法及記錄日志、調(diào)試等簡(jiǎn)單操作技巧,需要的朋友可以參考下2019-05-05
thinkphp配置文件路徑的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇thinkphp配置文件路徑的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-08-08
php用header函數(shù)實(shí)現(xiàn)301跳轉(zhuǎn)代碼實(shí)例
分享一個(gè)php 301跳轉(zhuǎn)的代碼,很簡(jiǎn)單,主要是用header函數(shù)實(shí)現(xiàn)轉(zhuǎn),大家可以參考使用2013-11-11
php獲取目標(biāo)函數(shù)執(zhí)行時(shí)間示例
這篇文章主要介紹了php獲取目標(biāo)函數(shù)執(zhí)行時(shí)間示例,需要的朋友可以參考下2014-03-03
PHP統(tǒng)計(jì)nginx訪問(wèn)日志中的搜索引擎抓取404鏈接頁(yè)面路徑
這篇文章主要介紹了PHP統(tǒng)計(jì)nginx訪問(wèn)日志中的搜索引擎抓取404鏈接頁(yè)面路徑,可以對(duì)每個(gè)搜索引擎單獨(dú)統(tǒng)計(jì),需要的朋友可以參考下2014-06-06
thinkphp解決數(shù)據(jù)傳入數(shù)據(jù)庫(kù)中特殊字符的問(wèn)題小結(jié)
這篇文章主要介紹了thinkphp解決數(shù)據(jù)傳入數(shù)據(jù)庫(kù)中特殊字符的問(wèn)題,為了解決這個(gè)問(wèn)題,你需要確保在插入數(shù)據(jù)庫(kù)之前,不對(duì)文本內(nèi)容進(jìn)行HTML實(shí)體編碼,需要的朋友可以參考下2024-03-03
編寫PHP程序檢查字符串中的中文字符個(gè)數(shù)的實(shí)例分享
這篇文章主要介紹了編寫PHP程序檢查字符串中的中文字符個(gè)數(shù)的實(shí)例分享,文中利用了PHP中mb_strlen函數(shù)的實(shí)現(xiàn)原理,需要的朋友可以參考下2016-03-03
PHP中__get()和__set()的用法實(shí)例詳解
在PHP5中,預(yù)定義了兩個(gè)函數(shù)“__get()”和“__set()”來(lái)獲取和賦值其屬性,對(duì)每個(gè)字段進(jìn)行set和get的操作。只需要加上兩個(gè)魔術(shù)方法即可2013-06-06

