一、HTML
1
二、JS
说明:需要引入layui中的table和laytpl模板引擎,laytpl可以自定义事件及自定义数据字段等
1 2 5 6 7 10 11
1
三、PHP
1 #这里是PHP类中主要的配合步骤 2 3 # 接收layui发送的limit 4 if (trim($_GET['limit'])) { 5 $limit = trim($_GET['limit']); 6 }else{ 7 $limit = 15; 8 } 9 10 # 按某字段排序,$rows为数据数组11 $sort_num = array_column($rows,'num');12 array_multisort($sort_num,SORT_DESC,$rows, SORT_DESC);13 14 # 调用自定义分页函数15 $datas = array();16 $datas = showpage($rows,$limit);17 18 $items = array();19 20 # 返回layui数据格式21 $items['data'] = $datas['rows'];22 $items['code'] = 0;23 $items['msg'] = 'ok';24 $items['count'] = $datas['tot'];25 26 exit(json_encode($items));
1 # showpage函数 2 3 function showpage($rows,$count){ 4 $tot = count($rows); // 总数据条数 5 6 if ($_GET['page']) { //获取当前页码 7 $page = $_GET['page']; 8 }else{ 9 $page = 1;10 }11 12 // $count = $count; # 每页显示条数13 14 $countpage = ceil($tot/$count); # 计算总共页数15 16 $start = ($page-1)*$count; # 计算每页开始位置17 18 $datas = array_slice($rows, $start, $count); # 计算当前页数据19 20 # 获取上一页和下一页21 if ($page > 1) {22 $uppage = $page-1;23 }else{24 $uppage = 1;25 }26 27 if ($page < $countpage) {28 $nextpage = $page+1;29 }else{30 $nextpage = $countpage;31 }32 33 $pages['countpage'] = $countpage;34 $pages['page'] = $page;35 $pages['uppage'] = $uppage;36 $pages['nextpage'] = $nextpage;37 $pages['tot'] = $tot;38 39 //循环加入序号 , 避免使用$i引起的序号跳位40 $n = 1;41 foreach ($datas as &$data) {42 $data['n'] = $n;43 $n++;44 }45 46 $pages['rows'] = $datas;47 48 return $pages;49 }