Laravel5.5 手动分页和自定义分页样式的简单实现(3)

经过思考,我们不去改laravel框架的源代码,可以通过重构render方法或者重新定义一个生成HTML模板的方法来实现自定义HTML模板

因为我们只需要自定义HTML模板,所以,可以创建一个文件,继承\Illuminate\Contracts\Pagination\LengthAwarePaginator 类

看代码:

<?php
 
namespace App\Helpers;
 
use Illuminate\Pagination\LengthAwarePaginator;
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2018/4/9
 * Time: 9:08
 */
class Newpage extends LengthAwarePaginator {
 
  public $de_page = 10; //默认显示分页数
  public $pageHtml;
 
  public function newrender(){
    if($this->hasPages())
    {
 
      return sprintf("<ul class='pagination'>%s %s %s</ul>",
        $this->pre_page(),
        $this->pages_num(),
        $this->next_page()
      );
    }
  }
 
  #上一页
  public function pre_page(){
    if($this->currentPage == 1){
      //dd($this->currentPage);
      return "<li class='disabled'><span>《</span></li>";
    }else{
 
      $url = $this->path."&page=".($this->currentPage-1);
      //dd($url);
      return "<li><a href=".$url." rel="external nofollow" rel='prev'>《</a></li>";
    }
  }
 
  #页码
  public function pages_num(){
    $pages = '';
    if($this->currentPage <= 6){
      for($i = 1; $i <= $this->de_page; $i++){
        if($this->currentPage == $i){
          $pages .= "<li class='active'><a href=".$this->path." rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" &page=".$i.">".$i."</a></li>";
        }else{
          $pages .="<li><a href=".$this->path." rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" &page=".$i.">".$i."</a></li>";
        }
      }
    }else{
      #当前页前边部分
      for($i = 5; $i >=1 ; $i--){
        $url =$this->currentPage-$i;
        $pages .= "<li><a href=".$this->path." rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" &page=".$url.">".$url."</a></li>";
      }
      #当前页
      $pages .= "<li class='active'><span>".$this->currentPage."</span></li>";
      #当前页后边部分
      for($i = 1;$i < 5; $i++ ){
        $nowpage =$this->currentPage+$i;
        $pages .= "<li><a href=".$this->path." rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" &page=".$nowpage.">".$nowpage."</a></li>";
      }
    }
    return $pages;
 
  }
  #下一页
  public function next_page(){
    if($this->currentPage < $this->total){
      $page =$this->currentPage+1;
      return "<li><a href=".$this->path." rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" &page=".$page." rel='next'><span>》</span></a></li>";
    }else{
      return "<li class='disabled'><span>》</span></li>";
    }
 
  }
 
}

      

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/5382.html