实现分页效果如下:
以下标注红色字体的为重点
找到文件page.class.php在ThinkPHP/Library/Thinkpage.class.php并打开文件,复制函数show,在本文件中黏贴并改成你自定义的函数名,在此我更改为show1,更改上一页,下一页,第一页和最后一页的逻辑输出,将数字链接for循环删除,其他不变,更改如下:
public function show1() { if(0 == $this->totalRows) return ''; /* 生成URL */ $this->parameter[$this->p] = '[PAGE]'; $this->url = U(ACTION_NAME, $this->parameter); /* 计算分页信息 */ $this->totalPages = ceil($this->totalRows / $this->listRows); //总页数 if(!empty($this->totalPages) && $this->nowPage > $this->totalPages) { $this->nowPage = $this->totalPages; } /* 计算分页临时变量 */ $now_cool_page = $this->rollPage/2; $now_cool_page_ceil = ceil($now_cool_page); $this->lastSuffix && $this->config['last'] = $this->totalPages; //上一页 $up_row = $this->nowPage - 1; $up_page = $up_row > 0 ? '<a class="prev" href="' . $this->url($up_row) . '">' . 上一页 . '</a>' : '<a class="prev" href="javascript:(0);">' . 上一页 . '</a>'; //下一页 $down_row = $this->nowPage + 1; $down_page = ($down_row <= $this->totalPages) ? '<a class="next" href="' . $this->url($down_row) . '">' .下一页 . '</a>' : '<a class="next" href="javascript:(0);">' .下一页 . '</a>'; //第一页 $the_first=$this->nowPage > 1 ? '<a class="first" href="' . $this->url(1) . '">' . 首页 . '</a>' : '<a class="first" href="javascript:(0);">' . 首页 . '</a>'; //最后一页 $the_end = $this->nowPage < $this->totalPages ? '<a class="end" href="' . $this->url($this->totalPages) . '">' .末页. '</a>' : '<a class="end" href="javascript:(0);">' .末页. '</a>'; //数字连接 $link_page = ""; //替换分页内容 $page_str = str_replace( array('%HEADER%', '%NOW_PAGE%', '%UP_PAGE%', '%DOWN_PAGE%', '%FIRST%', '%LINK_PAGE%', '%END%', '%TOTAL_ROW%', '%TOTAL_PAGE%'), array($this->config['header'], $this->nowPage, $up_page, $down_page, $the_first,$link_page, $the_end, $this->totalRows, $this->totalPages), $this->config['theme']); return "{$page_str}"; }
在定义的model中定义查询数据库中数据并分页
public function getDocPageList($table,$where,$p, $pageSize,$order='docid desc'){ $list= M($table)->where($where)->order($order) ->page($p.','.$pageSize) ->select(); $count = M($table)->field('*') ->where($where) ->order($order) ->count(); $Page = new \Think\Page($count, $pageSize); $module_name = '/' . MODULE_NAME . '/' . CONTROLLER_NAME . '/' . ACTION_NAME;//此处为获取路径,模块名/控制器名/方法名 //$page便是上面效果的静态代码显示
$Page->setConfig('header','<p><span>%NOW_PAGE%</span>/<span title="%TOTAL_PAGE%" class="all">%TOTAL_PAGE%</span></p> <p>转到<input type="text" id="page_num" class="txt_page" />页</p> <a class="gopage" href="'.$module_name.'/p/">GO</a>'); $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%'); $show = $Page->show1(); $result['pages'] = $show; $result['list'] = $list; $result['count'] = $count; return $result; }
以上分页的css样式如下
/*分页*/ .d_page{width: 700px;clear: both;margin:0px auto;padding-top:20px;} .d_page a,.d_page p{display:inline-block;margin:0px 8px;} .d_page a,.d_page input{border:1px solid #e6e6e6;padding:5px 10px;} .d_page input{width:40px} .d_page a:hover{background:#d57a22;border:1px solid #d57a22;color:#fff;}
在控制器中调用上面model下的函数,此处我以查询user数据表为例:
//定义对象,引用控制器 public function __construct() { parent::__construct(); $this->docinfo_model = new \Common\Model\DocinfoModel(); } $result = $this->docinfo_model->getDocPageList('user',$where,$p, $pageSize,$order); $this->assign("doclist", $result['list']); $this->assign("count", $result['count']); $this->assign("pages", $result['pages']); $this->display();
在模板中输出的时候直接输出pages
<div>{$pages}</div>
使用jquery实现分页的跳转
<script src='__PUBLIC__/js/jquery-1.8.3.min.js'></script>
<script> $(function(){ //分页跳转 $('#page_num').blur(function(){ var url=$('.gopage').attr('href'); var strs=url.substr(0); var value=this.value; var all=$('.all').attr("title"); var i=parseFloat(all);//将字符转化为数字 if(value<=i && !isNaN(value) && value>0){ value=Math.ceil(value); }else{ value=''; } var nuwurl=strs+value; $(".gopage").attr("href",nuwurl); }) }); </script>