public $page = ""; public $size = ""; public function getPageAndSize($data){ $this->page = empty($data['page'])?"1":$data['page']; $this->size = empty($data['limit']) ?"2":$data['limit']; }
public function index(){ $newsModel = new NewsModel(); //模式一 // $lists = $newsModel->getNews(); $data = $this->request->param(); $whereData = []; $this->getPageAndSize($data); $whereData['page'] = $this->page; $whereData['size'] = $this->size; $lists = $newsModel->getNewsByCondition($whereData); $total = $newsModel->getTotalNews($whereData);//总条数 $total_page = ceil($total/$whereData['size']);//总页数 $catModel = new Cat(); $cat = $catModel->getNormalCatLists(['status' => 1]); $cat = array_column($cat,'cat_name','id'); foreach ($lists as $key=>$value){ $lists[$key]['cat_name'] = $cat[$value['catid']]; } $this->assign('size',$whereData['size']); $this->assign('page',$whereData['page']); $this->assign('total_page',$total_page); $this->assign('total',$total); $this->assign('lists',$lists); return $this->fetch(); }
public function getNewsByCondition($param){ $condition['status'] =['neq','-1']; $order = ['id'=>'desc']; $from = ($param['page'] - 1)*$param['size'];//开始页 $result = $this->where($condition) ->limit($from,$param['size']) ->order($order) ->select(); // echo $this->getLastSql(); return $result; } public function getTotalNews(){ $condition['status'] =['neq','-1']; return $this->where($condition)->count(); }
<div id="test1"></div> <script type="text/javascript" src="__STATIC__/js/layui/dist/layui.js"></script> <script>
layui.use(['laypage', 'layer'], function(){ var laypage = layui.laypage ,layer = layui.layer; var limits = [1,2,3];
var url ='{:url("news/index")}'; laypage.render({ elem: 'test1', count: "{$total}", limit:"{$size}", limits:limits, curr: '{$page}'|| 1, //当前页 layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'] ,jump: function(obj,first){ if(!first){ //一定要加此判断,否则初始时会无限刷新 //alert(url+'?page='+e.curr); location.href = url+'&page='+obj.curr+'&limit='+obj.limit; } } }); }) </script>