ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
LayUI实现分页知识点 [TOC] ## 1、封装分页方法 ~~~ // 分页 public function pages($pageSize = 10){ $total = Db::name($this->table)->where($this->where)->count(); $query = Db::name($this->table)->field($this->field)->where($this->where); $this->order && $query = $query->order($this->order); $data = $query->paginate($pageSize,$total); return array('total'=>$total,'lists'=>$data->items(),'pages'=>$data->render()); } ~~~ 备注: paginate()是TP5自带的方法,两个参数:一个是每页显示条数,一个是总数。 pages()自定义封装方法,一个参数:每页显示条数,返回三个值(分别是:总数、数据、分页) ~~~ // 指定表名 public function table($table){ $this->where = array(); $this->field = '*'; $this->order = ''; $this->table = $table; return $this; } ~~~ ## 2、自定义分页调用 影片列表 ~~~ public function index(){ $data['pageSize'] = 15; $data['page'] = max(1,(int)input('get.page')); $data['wd'] = trim(input('get.wd')); $where = array(); $data['wd'] && $where = 'title like "%'.$data['wd'].'%"'; $data['data'] = $this->db->table('video')->where($where)->order('id desc')->pages($data['pageSize']); $label_ids = []; foreach ($data['data']['lists'] as $item) { !in_array($item['channel_id'],$label_ids) && $label_ids[] = $item['channel_id']; !in_array($item['charge_id'],$label_ids) && $label_ids[] = $item['charge_id']; !in_array($item['area_id'],$label_ids) && $label_ids[] = $item['area_id']; } $label_ids && $data['labels'] = $this->db->table('video_label')->where('id in('.implode(',',$label_ids).')')->cates('id'); $this->assign('data',$data); return $this->fetch(); } ~~~ ## 3、分页组件 ### (1)引入组件 ~~~ <link rel="stylesheet" type="text/css" href="__STATIC__/plugins/layui/css/layui.css"> <script type="text/javascript" src="__STATIC__/plugins/layui/layui.js"></script> ~~~ ### (2)实例化组件 ~~~ <script type="text/javascript"> layui.use(['layer','laypage'],function(){ layer = layui.layer; $ = layui.jquery; laypage = layui.laypage; laypage.render({ elem: 'pages' ,count:{$data.data.total} ,limit:{$data.pageSize} ,curr:{$data.page} ,jump: function(obj, first){ if(!first){ searchs(obj.curr); } } }); }); </script> ~~~ ### (3)渲染组件 ~~~ <div id="pages"></div> ~~~