用layui根据以下代码 写分页功能 public function index(){ $data = []; //数据总数 $map = []; $status = input('status'); if($status){ $map['status'] = $status; } $typeId = input('typeId'); if($typeId){ $map['typeId'] = $typeId; } $page = input('page',1); $pageSize = input('pageSize',2); $list = AboutuslistMod::field('*')->where($map)->paginate($pageSize,false,['query' => request()->param()]); if(!empty($list)){ $list = $list->toArray(); $total = $list['total']; $data = $list['data']; } unset($list); $typeList = AboutuslistMod::getType(); if(!empty($data)){ $returnData = [ 'code' => 0, 'count' => $total, 'page' => $page, 'pageSize' => $pageSize, 'data' => $data, ]; $data=json(['code'=>0,'msg'=>'获取数据成功!','count'=>$total,'data'=>$data]); } $this->assign([ 'page' => $page, 'pageSize' => $pageSize, "total"=> $total, "list"=> $data, "typeList" => $typeList, // "pages"=> $list->render() ]); return $this->fetch(); }
时间: 2023-12-10 20:42:45 浏览: 139
以下是使用 layui 的分页功能的示例代码:
```
public function index()
{
$data = [];
// 数据总数
$map = [];
$status = input('status');
if ($status) {
$map['status'] = $status;
}
$typeId = input('typeId');
if ($typeId) {
$map['typeId'] = $typeId;
}
$page = input('page', 1);
$pageSize = input('pageSize', 2);
$list = AboutuslistMod::field('*')->where($map)->paginate($pageSize, false, ['query' => request()->param()]);
if (!empty($list)) {
$list = $list->toArray();
$total = $list['total'];
$data = $list['data'];
}
unset($list);
$typeList = AboutuslistMod::getType();
if (!empty($data)) {
$returnData = [
'code' => 0,
'count' => $total,
'page' => $page,
'pageSize' => $pageSize,
'data' => $data,
];
$data = json(['code'=>0,'msg'=>'获取数据成功!','count'=>$total,'data'=>$data]);
}
$this->assign([
'page' => $page,
'pageSize' => $pageSize,
"total"=> $total,
"list"=> $data,
"typeList" => $typeList,
]);
// 分页渲染
$render = $list->render();
$this->assign('pages', $render);
return $this->fetch();
}
```
在 HTML 模板中,通过以下代码来渲染分页:
```
<div id="page" style="text-align:center;"></div>
<script type="text/javascript">
layui.use(['laypage'], function(){
var laypage = layui.laypage;
laypage.render({
elem: 'page',
count: <?php echo $total; ?>,
limit: <?php echo $pageSize; ?>,
curr: <?php echo $page; ?>,
layout: ['prev', 'page', 'next', 'skip'],
jump: function (obj, first) {
if (!first) {
location.href = '<?php echo url("index"); ?>' + '?page=' + obj.curr + '&pageSize=' + obj.limit;
}
}
});
});
</script>
```
阅读全文