引入datatables
<!--第一步:引入Javascript / CSS (CDN)-->
<!-- DataTables CSS -->
<link rel="stylesheet" type="text/css" href="http://cdn.datatables.net/1.10.21/css/jquery.dataTables.css">
<!-- jQuery -->
<script type="text/javascript" charset="utf8" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<!-- DataTables -->
<script type="text/javascript" charset="utf8" src="http://cdn.datatables.net/1.10.21/js/jquery.dataTables.js"></script>
?
script
var lang = {
"sProcessing": "处理中...",
"sLengthMenu": "每页 _MENU_ 项",
"sZeroRecords": "没有匹配结果",
"sInfo": "当前显示第 _START_ 至 _END_ 项,共 _TOTAL_ 项。",
"sInfoEmpty": "当前显示第 0 至 0 项,共 0 项",
"sInfoFiltered": "(由 _MAX_ 项结果过滤)",
"sInfoPostFix": "",
"sSearch": "搜索:",
"sUrl": "",
"sEmptyTable": "表中数据为空",
"sLoadingRecords": "载入中...",
"sInfoThousands": ",",
"oPaginate": {
"sFirst": "首页",
"sPrevious": "上页",
"sNext": "下页",
"sLast": "末页",
"sJump": "跳转"
},
"oAria": {
"sSortAscending": ": 以升序排列此列",
"sSortDescending": ": 以降序排列此列"
}
};
//初始化表格
<!--第三步:初始化Datatables-->
$(document).ready( function () {
$('#example').DataTable({
// 开启分页
// paging:true,
// scrollY:500,
//每页显示条数
lengthMenu:[10,25],
searching: true,
//开启服务器模式
serverSide: true,
//后端请求
ajax:{
url:'{{route('admin.index')}}',
type:'get'
},
//需要展示页面的数据
columns:[
{ "data": "id" },
{ "data": "truename" },
{ "data": "sex" }
]
});
});
php代码?
public function index(Request $request){
if ($request->ajax()){
//datatables 渲染页面
//查询数据库数据
$data = UserModel::all();
//记录总条数
$total = $data->count();
//分页开始的位置
$start = $request->get('start');
//分页结束的位置
$length = $request->get('length');
//接收要搜索的数据
$search = $request->get('search');
//分页
$data = UserModel::when($search,function ($query,$search){
$query->where('truename','like','%'.$search['value'].'%');
})->offset($start)->limit($length)->get()->toArray();
// datatables 返回参数必选 一下四个
$result = [
'draw'=>$request->get('draw'),
'recordsTotal'=>$total,
'recordsFiltered'=>$total,
'data'=>$data
];
return $result;
}
return view('week.index');
}
|