TP5之自定义分页样式

  1. 分页样式为
  2. 在extend\目录下创建page目录,在page目录下创建Page.php文件,将以下代码放入文件中。
  3.   1 php
      2 namespace page;
      3 
      4 use think\Paginator;
      5 
      6 class Page extends Paginator
      7 {
      8 
      9     //首页
     10     protected function home() {
     11         if ($this->currentPage() > 1) {
     12             return "首页";
     13         } else {
     14             return "

    首页

    "; 15 } 16 } 17 18 //上一页 19 protected function prev() { 20 if ($this->currentPage() > 1) { 21 return "上一页"; 22 } else { 23 return "

    上一页

    "; 24 } 25 } 26 27 //下一页 28 protected function next() { 29 if ($this->hasMore) { 30 return "下一页"; 31 } else { 32 return"

    下一页

    "; 33 } 34 } 35 36 //尾页 37 protected function last() { 38 if ($this->hasMore) { 39 return "尾页"; 40 } else { 41 return "

    尾页

    "; 42 } 43 } 44 45 //统计信息 46 protected function info(){ 47 return "

    " . $this->lastPage . 48 "" . $this->total . "条数据

    "; 49 } 50 51 /** 52 * 页码按钮 53 * @return string 54 */ 55 protected function getLinks() 56 { 57 58 $block = [ 59 'first' => null, 60 'slider' => null, 61 'last' => null 62 ]; 63 64 $side = 3; 65 $window = $side * 2; 66 67 if ($this->lastPage < $window + 6) { 68 $block['first'] = $this->getUrlRange(1, $this->lastPage); 69 } elseif ($this->currentPage <= $window) { 70 $block['first'] = $this->getUrlRange(1, $window + 2); 71 $block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage); 72 } elseif ($this->currentPage > ($this->lastPage - $window)) { 73 $block['first'] = $this->getUrlRange(1, 2); 74 $block['last'] = $this->getUrlRange($this->lastPage - ($window + 2), $this->lastPage); 75 } else { 76 $block['first'] = $this->getUrlRange(1, 2); 77 $block['slider'] = $this->getUrlRange($this->currentPage - $side, $this->currentPage + $side); 78 $block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage); 79 } 80 81 $html = ''; 82 83 if (is_array($block['first'])) { 84 $html .= $this->getUrlLinks($block['first']); 85 } 86 87 if (is_array($block['slider'])) { 88 $html .= $this->getDots(); 89 $html .= $this->getUrlLinks($block['slider']); 90 } 91 92 if (is_array($block['last'])) { 93 $html .= $this->getDots(); 94 $html .= $this->getUrlLinks($block['last']); 95 } 96 97 return $html; 98 } 99 100 /** 101 * 渲染分页html 102 * @return mixed 103 */ 104 public function render() 105 { 106 if ($this->hasPages()) { 107 if ($this->simple) { 108 return sprintf( 109 '%s', 110 $this->css(), 111 $this->prev(), 112 $this->getLinks(), 113 $this->next() 114 ); 115 } else { 116 return sprintf( 117 '%s', 118 $this->css(), 119 $this->home(), 120 $this->prev(), 121 $this->getLinks(), 122 $this->next(), 123 $this->last(), 124 $this->info() 125 ); 126 } 127 } 128 } 129 130 /** 131 * 生成一个可点击的按钮 132 * 133 * @param string $url 134 * @param int $page 135 * @return string 136 */ 137 protected function getAvailablePageWrapper($url, $page) 138 { 139 return 'htmlentities($url) . '" title="第"'. $page .'"页" >' . $page . ''; 140 } 141 142 /** 143 * 生成一个禁用的按钮 144 * 145 * @param string $text 146 * @return string 147 */ 148 protected function getDisabledTextWrapper($text) 149 { 150 return '

    ' . $text . '

    '; 151 } 152 153 /** 154 * 生成一个激活的按钮 155 * 156 * @param string $text 157 * @return string 158 */ 159 protected function getActivePageWrapper($text) 160 { 161 return '' . $text . ''; 162 } 163 164 /** 165 * 生成省略号按钮 166 * 167 * @return string 168 */ 169 protected function getDots() 170 { 171 return $this->getDisabledTextWrapper('...'); 172 } 173 174 /** 175 * 批量生成页码按钮. 176 * 177 * @param array $urls 178 * @return string 179 */ 180 protected function getUrlLinks(array $urls) 181 { 182 $html = ''; 183 184 foreach ($urls as $page => $url) { 185 $html .= $this->getPageLinkWrapper($url, $page); 186 } 187 188 return $html; 189 } 190 191 /** 192 * 生成普通页码按钮 193 * 194 * @param string $url 195 * @param int $page 196 * @return string 197 */ 198 protected function getPageLinkWrapper($url, $page) 199 { 200 if ($page == $this->currentPage()) { 201 return $this->getActivePageWrapper($page); 202 } 203 204 return $this->getAvailablePageWrapper($url, $page); 205 } 206 207 /** 208 * 分页样式 209 */ 210 protected function css(){ 211 return ' '; 274 } 275 }

     

    修改  application\config.php  中的配置文件即可

 

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

 

over!over!over!

 

转载于:https://www.cnblogs.com/yulongcode/p/10580265.html

你可能感兴趣的:(TP5之自定义分页样式)