ThinkPHP5自定义分页样式

1.在thinkphp/library/think/paginator/driver目录下新建文件Page.php

注意命名空间和继承

1<?php 2namespace think\paginator\driver; 3 4use think\Paginator; 5class Page extends Paginator 6{ 7 public $rollPage=5;//分页栏每页显示的页数 8 9 //首页 10 protected function home() { 11 if ($this->currentPage() > 1) { 12 return "<a href='" . $this->url(1) . "' title='首页'>首页</a>"; 13 } else { 14 return "<p>首页</p>"; 15 } 16 } 17 //上一页 18 protected function prev() { 19 if ($this->currentPage() > 1) { 20 return "<a href='" . $this->url($this->currentPage - 1) . "' title='上一页'>上一页</a>"; 21 } else { 22 return "<p>上一页</p>"; 23 } 24 } 25 //下一页 26 protected function next() { 27 if ($this->hasMore) { 28 return "<a href='" . $this->url($this->currentPage + 1) . "' title='下一页'>下一页</a>"; 29 } else { 30 return"<p>下一页</p>"; 31 } 32 } 33 //尾页 34 protected function last() { 35 if ($this->hasMore) { 36 return "<a href='" . $this->url($this->lastPage) . "' title='尾页'>尾页</a>"; 37 } else { 38 return "<p>尾页</p>"; 39 } 40 } 41 //统计信息 42 protected function info(){ 43 return "<p class='pageRemark'>共<b>" . $this->lastPage . 44 "</b>页<b>" . $this->total . "</b>条数据</p>"; 45 } 46 /** 47 * 页码按钮 48 * @return string 49 */ 50 protected function getLinks() 51 { 52 if ($this->simple) 53 return ''; 54 $block = [ 55 'first' => null, 56 'slider' => null, 57 'last' => null 58 ]; 59 $rollPage = $this->rollPage;//分页栏每页显示的页数 60 $nowPage = floor($rollPage/2);//计算分页临时变量 61 62 if($this->lastPage <= $rollPage){ 63 $block['first'] = $this->getUrlRange(1, $this->lastPage); 64 }else if($this->currentPage <= $nowPage){ 65 $block['first'] = $this->getUrlRange(1, $rollPage); 66 }else if($this->currentPage >= ($this->lastPage - $nowPage)){ 67 $block['first'] = $this->getUrlRange($this->lastPage - $rollPage+1, $this->lastPage); 68 }else{ 69 $block['first'] = $this->getUrlRange($this->currentPage - $nowPage, $this->currentPage + $nowPage); 70 } 71 $html = ''; 72 if (is_array($block['first'])) { 73 $html .= $this->getUrlLinks($block['first']); 74 } 75 return $html; 76 } 77 /** 78 * 渲染分页html 79 * @return mixed 80 */ 81 public function render() 82 { 83 if ($this->hasPages()) { 84 if ($this->simple) { 85 return sprintf( 86 '%s<div class="pagination">%s %s %s</div>', 87 $this->css(), 88 $this->prev(), 89 $this->getLinks(), 90 $this->next() 91 ); 92 } else { 93 return sprintf( 94 '%s<div class="pagination">%s %s %s %s %s %s</div>', 95 $this->css(), 96 $this->home(), 97 $this->prev(), 98 $this->getLinks(), 99 $this->next(), 100 $this->last(), 101 $this->info() 102 ); 103 } 104 } 105 } 106 /** 107 * 生成一个可点击的按钮 108 * 109 * @param string $url 110 * @param int $page 111 * @return string 112 */ 113 protected function getAvailablePageWrapper($url, $page) 114 { 115 return '<a href="' . htmlentities($url) . '" title="第"'. $page .'"页" >' . $page . '</a>'; 116 } 117 /** 118 * 生成一个禁用的按钮 119 * 120 * @param string $text 121 * @return string 122 */ 123 protected function getDisabledTextWrapper($text) 124 { 125 return '<p class="pageEllipsis">' . $text . '</p>'; 126 } 127 /** 128 * 生成一个激活的按钮 129 * 130 * @param string $text 131 * @return string 132 */ 133 protected function getActivePageWrapper($text) 134 { 135 return '<a href="" class="cur">' . $text . '</a>'; 136 } 137 /** 138 * 生成省略号按钮 139 * 140 * @return string 141 */ 142 protected function getDots() 143 { 144 return $this->getDisabledTextWrapper('...'); 145 } 146 /** 147 * 批量生成页码按钮. 148 * 149 * @param array $urls 150 * @return string 151 */ 152 protected function getUrlLinks(array $urls) 153 { 154 $html = ''; 155 foreach ($urls as $page => $url) { 156 $html .= $this->getPageLinkWrapper($url, $page); 157 } 158 return $html; 159 } 160 /** 161 * 生成普通页码按钮 162 * 163 * @param string $url 164 * @param int $page 165 * @return string 166 */ 167 protected function getPageLinkWrapper($url, $page) 168 { 169 if ($page == $this->currentPage()) { 170 return $this->getActivePageWrapper($page); 171 } 172 return $this->getAvailablePageWrapper($url, $page); 173 } 174 /** 175 * 分页样式 176 */ 177 protected function css(){ 178 return ' <style type="text/css"> 179 .pagination p{ 180 margin:0; 181 cursor:pointer 182 } 183 .pagination{ 184 height:40px; 185 padding:20px 0px; 186 } 187 .pagination a{ 188 display:block; 189 float:left; 190 margin-right:10px; 191 padding:2px 12px; 192 height:24px; 193 border:1px #cccccc solid; 194 background:#fff; 195 text-decoration:none; 196 color:#808080; 197 font-size:12px; 198 line-height:24px; 199 } 200 .pagination a:hover{ 201 color:#077ee3; 202 background: white; 203 border:1px #077ee3 solid; 204 } 205 .pagination a.cur{ 206 border:none; 207 background:#077ee3; 208 color:#fff; 209 } 210 .pagination p{ 211 float:left; 212 padding:2px 12px; 213 font-size:12px; 214 height:24px; 215 line-height:24px; 216 color:#bbb; 217 border:1px #ccc solid; 218 background:#fcfcfc; 219 margin-right:8px; 220 } 221 .pagination p.pageRemark{ 222 border-style:none; 223 background:none; 224 margin-right:0px; 225 padding:4px 0px; 226 color:#666; 227 } 228 .pagination p.pageRemark b{ 229 color:red; 230 } 231 .pagination p.pageEllipsis{ 232 border-style:none; 233 background:none; 234 padding:4px 0px; 235 color:#808080; 236 } 237 .dates li {font-size: 14px;margin:20px 0} 238 .dates li span{float:right} 239 </style>'; 240 } 241}

2.修改application/config配置文件

1//分页配置 2 'paginate' => [ 3 'type' => 'Page', 4 'var_page' => 'page', 5 'list_rows' => 15, 6 ],

效果:限制分页栏每页显示的5页

点击4时会显示2-6页,同理,点击5时会显示3-7页,即每次只显示5个页码

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )