您好,登錄后才能下訂單哦!
本篇文章為大家展示了怎么在Laravel5.5中實現手動分頁和自定義分頁樣式,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
一、創建手動分頁
在laravel自帶的分頁中,一般是通過數據庫查詢訪問paginate()方法來達到分頁的效果 ,like this:
class IndexControllerextends Controller
{ publicfunctionindex() { $person = DB::table('person')->paginate(15); return view('index.pagTest',['person'=> $person]); } }
查看框架的分頁源代碼
#vender/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php
/** * Paginate the given query. * * @param int $perPage * @param array $columns * @param string $pageName * @param int|null $page * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator * * @throws \InvalidArgumentException */ public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null) { $page = $page ?: Paginator::resolveCurrentPage($pageName); $perPage = $perPage ?: $this->model->getPerPage(); $results = ($total = $this->toBase()->getCountForPagination()) ? $this->forPage($page, $perPage)->get($columns) : $this->model->newCollection(); return $this->paginator($results, $total, $perPage, $page, [ 'path' => Paginator::resolveCurrentPath(), 'pageName' => $pageName, ]); }
發現,分頁用了 \Illuminate\Contracts\Pagination\LengthAwarePaginator 構造方法,查看這個構造方法
<?php namespace Illuminate\Pagination; use Countable; use ArrayAccess; use JsonSerializable; use IteratorAggregate; use Illuminate\Support\Collection; use Illuminate\Support\HtmlString; use Illuminate\Contracts\Support\Jsonable; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Pagination\LengthAwarePaginator as LengthAwarePaginatorContract; class LengthAwarePaginator extends AbstractPaginator implements Arrayable, ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Jsonable, LengthAwarePaginatorContract { /** * The total number of items before slicing. * * @var int */ protected $total; /** * The last available page. * * @var int */ protected $lastPage; /** * Create a new paginator instance. * * @param mixed $items * @param int $total * @param int $perPage * @param int|null $currentPage * @param array $options (path, query, fragment, pageName) * @return void */ public function __construct($items, $total, $perPage, $currentPage = null, array $options = []) { foreach ($options as $key => $value) { $this->{$key} = $value; } $this->total = $total; $this->perPage = $perPage; $this->lastPage = max((int) ceil($total / $perPage), 1); $this->path = $this->path !== '/' ? rtrim($this->path, '/') : $this->path; $this->currentPage = $this->setCurrentPage($currentPage, $this->pageName); $this->items = $items instanceof Collection ? $items : Collection::make($items); }
如果要實現手動分頁,只需要使用這個構造方法,給定參數,就能達到分頁的效果
貼代碼:
public function setPage2(Request $request,$data,$prepage,$total){
#每頁顯示記錄 $prePage = $prepage; //$total =count($data); $allitem = $prepage *100; $total > $allitem ? $total = $allitem : $total; if(isset($request->page)){ $current_page =intval($request->page); $current_page =$current_page<=0?1:$current_page; }else{ $current_page = 1; } #url操作 $url = $url='http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]; if(strpos($url,'&page')) $url=str_replace('&page='.$request->page, '',$url); # $data must be array $item =array_slice($data,($current_page-1)*$prePage,$prePage); $paginator = new LengthAwarePaginator($item,$total,$prePage,$current_page,[ 'path'=>$url, 'pageName'=>'page' ]); return $paginator; }
($data 為需要進行分頁的數據)
說明:
1、在考慮到代碼的復用性,我將分頁代碼封裝到app/Controllers/Controller.php中的一個方法里面,這樣在其他控制器里只需要$this->setPage(Request $request,$data,$prepage,$total) 就能使用了,(前提:其他控制器繼承了Controller.php)
2、分頁的URL,因為我的項目的url一定會攜帶一個kw參數,所以我直接用str_replace替換"&page",如果是存在不攜參分頁的話,需要判斷,到底是"?page"還是"&page"。(url的邏輯可以自己寫)
#分頁 php
$paginator = $this->setPage2($request,$data,25,$sum); $data =$paginator->toArray()['data'];
在模板中:{{$paginator->render()}}即能輸出分頁HTML,樣式如下:
二、自定義分頁樣式
在實際開發中,不希望用戶在瀏覽時直接瀏覽最后幾頁,只想用戶從前往后依次的瀏覽,如百度搜索分頁,這時候,就想修改分頁的樣式,經過一個下午的奮戰,貼出解決過程
在上一環節中,手動創建了分頁,了解HTML的模板生成是render()方法,
#\Illuminate\Contracts\Pagination\LengthAwarePaginator
/** * Render the paginator using the given view. * * @param string|null $view * @param array $data * @return \Illuminate\Support\HtmlString */ public function render($view = null, $data = []) { return new HtmlString(static::viewFactory()->make($view ?: static::$defaultView, array_merge($data, [ 'paginator' => $this, 'elements' => $this->elements(), ]))->render()); }
經過思考,我們不去改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>"; } } }
我選擇的方法是自定義新的方法生成HTML模板,模板中通過:{{$paginator->newrender()}}輸出HTML
如果選擇重構render()方法,只需要將上面的newrender()方法做一些小變動
public function render($view=null,$data=[]){ if($this->hasPages()) { return sprintf("<ul class='pagination'>%s %s %s</ul>", $this->pre_page(), $this->pages_num(), $this->next_page() ); } }
模板中通過:{{$paginator->render()}}輸出HTML
最終效果如圖:
上述內容就是怎么在Laravel5.5中實現手動分頁和自定義分頁樣式,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。