首頁 > 後端開發 > php教程 > Laravel使用Pagination外掛程式實現自訂分頁

Laravel使用Pagination外掛程式實現自訂分頁

*文
發布: 2023-03-18 20:52:01
原創
2106 人瀏覽過

Laravel如何使用Pagination外掛程式實作自訂分頁?這篇文章主要介紹了PHP框架Laravel5.1外掛Pagination實作自訂分頁的相關資料,需要的朋友可以參考下。希望對大家有幫助。

Laravel 的分頁很方便,其實擴充起來也蠻容易的,下面就來做個範例,擴充一下paginate() 和simplePaginate() 方法,來實作我們自訂分頁樣式,像是顯示"上一頁" 和"下一頁" ,而不是"《" 和"》" ,當然擴展的方法掌握了你就可以肆無忌憚的擴展一個你想要的分頁了,比如跳轉到某一頁,分頁顯示總共多少記錄,目前顯示的記錄範圍等等巴拉巴拉的。 。 。

5.1和5.2應該是同樣的方法,我這裡用的是5.2的版本。文件告訴我們 Paginator 對應於查詢語句建構器和 Eloquent 的  simplePaginate 方法,而  LengthAwarePaginator 則等同於  paginate 方法。那我們還是來看下原始碼,具體這個  paginate 是如何實作render() 的,

Illuminate/Pagination/LengthAwarePaginator.php

#
<?php

namespace Illuminate\Pagination;

......

class LengthAwarePaginator extends AbstractPaginator implements Arrayable, ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Jsonable, LengthAwarePaginatorContract 
{
......
  public function render(Presenter $presenter = null)
  {
    if (is_null($presenter) && static::$presenterResolver) {
      $presenter = call_user_func(static::$presenterResolver, $this);
    }

    $presenter = $presenter ?: new BootstrapThreePresenter($this);

    return $presenter->render();
  }
......
}
登入後複製

render() 中傳入的是一個Presenter 的實例,並且呼叫這個實例化的render 方法來實現分頁的顯示的。如果沒有則呼叫BootstrapThreePresenter 中render() 的,來看看BootstrapThreePresenter 是幹嘛的

Illuminate/Pagination/BootstrapThreePresenter.php

<?php

namespace Illuminate\Pagination;

use Illuminate\Support\HtmlString; 
use Illuminate\Contracts\Pagination\Paginator as PaginatorContract; 
use Illuminate\Contracts\Pagination\Presenter as PresenterContract;

class BootstrapThreePresenter implements PresenterContract 
{
  use BootstrapThreeNextPreviousButtonRendererTrait, UrlWindowPresenterTrait;

  /**
   * The paginator implementation.
   *
   * @var \Illuminate\Contracts\Pagination\Paginator
   */
  protected $paginator;

  /**
   * The URL window data structure.
   *
   * @var array
   */
  protected $window;

  /**
   * Create a new Bootstrap presenter instance.
   *
   * @param \Illuminate\Contracts\Pagination\Paginator $paginator
   * @param \Illuminate\Pagination\UrlWindow|null $window
   * @return void
   */
  public function __construct(PaginatorContract $paginator, UrlWindow $window = null)
  {
    $this->paginator = $paginator;
    $this->window = is_null($window) ? UrlWindow::make($paginator) : $window->get();
  }

  /**
   * Determine if the underlying paginator being presented has pages to show.
   *
   * @return bool
   */
  public function hasPages()
  {
    return $this->paginator->hasPages();
  }

  /**
   * Convert the URL window into Bootstrap HTML.
   *
   * @return \Illuminate\Support\HtmlString
   */
  public function render()
  {
    if ($this->hasPages()) {
      return new HtmlString(sprintf(
        &#39;<ul class="pagination">%s %s %s</ul>&#39;,
        $this->getPreviousButton(),
        $this->getLinks(),
        $this->getNextButton()
      ));
    }

    return &#39;&#39;;
  }
......
}
登入後複製
strap#這裡可以看到BootstrapThreePresenter.php

##

<?php

namespace Illuminate\Contracts\Pagination;

interface Presenter 
{
  /**
   * Render the given paginator.
   *
   * @return \Illuminate\Contracts\Support\Htmlable|string
   */
  public function render();

  /**
   * Determine if the underlying paginator being presented has pages to show.
   *
   * @return bool
   */
  public function hasPages();
}
登入後複製
strap實作了PresenterContract 的接口, render() 才是分頁顯示的真正實現,建構方法中的第一個參數PaginatorContract 其實就是一個Paginator 我們繼續看下PresenterContract 也就是Presenter 介面中定義了什麼方法需要實作

illuminate/contracts/Pagination/Presenter.php


#
<?php 
namespace App\Foundations\Pagination;

use Illuminate\Contracts\Pagination\Presenter as PresenterContract; 
use Illuminate\Contracts\Pagination\LengthAwarePaginator as PaginatorContract; 
use Illuminate\Pagination\UrlWindow; 
use Illuminate\Support\HtmlString; 
use Illuminate\Pagination\BootstrapThreeNextPreviousButtonRendererTrait; 
use Illuminate\Pagination\UrlWindowPresenterTrait;

class CustomerPresenter implements PresenterContract 
{
  use BootstrapThreeNextPreviousButtonRendererTrait, UrlWindowPresenterTrait;

  protected $paginator;

  protected $window;

  /**
   * Create a new Bootstrap presenter instance.
   *
   * @param \Illuminate\Contracts\Pagination\Paginator $paginator
   * @param \Illuminate\Pagination\UrlWindow|null $window
   * @return void
   */
  public function __construct(PaginatorContract $paginator, UrlWindow $window = null)
  {
    $this->paginator = $paginator;
    $this->window = is_null($window) ? UrlWindow::make($paginator) : $window->get();
  }

  /**
   * Determine if the underlying paginator being presented has pages to show.
   *
   * @return bool
   */
  public function hasPages()
  {
    return $this->paginator->hasPages();
  }

  /**
   * Convert the URL window into Bootstrap HTML.
   *
   * @return \Illuminate\Support\HtmlString
   */
  public function render()
  {
    if ($this->hasPages()) {
      return new HtmlString(sprintf(
        &#39;<ul class="pagination">%s %s %s</ul>&#39;,
        $this->getPreviousButton(&#39;上一页&#39;),//具体实现可以查看该方法
        $this->getLinks(),
        $this->getNextButton(&#39;下一页&#39;)//具体实现可以查看该方法
      ));
    }

    return &#39;&#39;;
  }

  /**
   * Get HTML wrapper for an available page link.
   *
   * @param string $url
   * @param int $page
   * @param string|null $rel
   * @return string
   */
  protected function getAvailablePageWrapper($url, $page, $rel = null)
  {
    $rel = is_null($rel) ? &#39;&#39; : &#39; rel="&#39; . $rel . &#39;"&#39;;

    return &#39;<li><a href="&#39; . htmlentities($url) . &#39;"&#39; . $rel . &#39;>&#39; . $page . &#39;</a></li>&#39;;
  }

  /**
   * Get HTML wrapper for disabled text.
   *
   * @param string $text
   * @return string
   */
  protected function getDisabledTextWrapper($text)
  {
    return &#39;<li class="disabled hide"><span>&#39; . $text . &#39;</span></li>&#39;;
  }

  /**
   * Get HTML wrapper for active text.
   *
   * @param string $text
   * @return string
   */
  protected function getActivePageWrapper($text)
  {
    return &#39;<li class="active"><span>&#39; . $text . &#39;</span></li>&#39;;
  }

  /**
   * Get a pagination "dot" element.
   *
   * @return string
   */
  protected function getDots()
  {
    return $this->getDisabledTextWrapper(&#39;...&#39;);
  }

  /**
   * Get the current page from the paginator.
   *
   * @return int
   */
  protected function currentPage()
  {
    return $this->paginator->currentPage();
  }

  /**
   * Get the last page from the paginator.
   *
   * @return int
   */
  protected function lastPage()
  {
    return $this->paginator->lastPage();
  }

}
登入後複製

其中定義了render 和hasPages 方法需要實作

好了,那我們現在已經很清晰了,我們要自訂分頁的顯示,那麼就要寫一個我們自己的Presenter 來實作介面中的render() 和hasPages() 就可以了。

首先就來簡單的實作一個paginate(),顯示出來"上一頁"和"下一頁",中間是分頁數字的例子。

新檔案如下(個人習慣)

app/Foundations/Pagination/CustomerPresenter.php

<?php 
namespace App\Foundations\Pagination;


use Illuminate\Support\HtmlString; 
use Illuminate\Contracts\Pagination\Paginator as PaginatorContract;

class CustomerSimplePresenter extends CustomerPresenter 
{
  /**
   * Create a simple Bootstrap 3 presenter.
   *
   * @param \Illuminate\Contracts\Pagination\Paginator $paginator
   * @return void
   */
  public function __construct(PaginatorContract $paginator)
  {
    $this->paginator = $paginator;
  }

  /**
   * Determine if the underlying paginator being presented has pages to show.
   *
   * @return bool
   */
  public function hasPages()
  {
    return $this->paginator->hasPages() && count($this->paginator->items()) > 0;
  }

  /**
   * Convert the URL window into Bootstrap HTML.
   *
   * @return \Illuminate\Support\HtmlString
   */
  public function render()
  {
    if ($this->hasPages()) {
      return new HtmlString(sprintf(
        &#39;<ul class="pager">%s %s</ul>&#39;,
        $this->getPreviousButton(&#39;上一篇&#39;),
        $this->getNextButton(&#39;下一篇&#39;)
      ));
    }

    return &#39;&#39;;
  }

}
登入後複製

就這麼簡單,主要就是render()方法,如果項目中需要修改分頁樣式,或者添加分頁跳轉之類的需求只要重寫其中的各項顯示的方法中的html元素就可以了,很靈活,在blade模板中也需要修該,比如我們的Paginator 叫$users ,預設的分頁顯示是這樣的:

{!! $users->render() !!}修改成我們自訂後的分頁顯示:

{!! with(new \App\Foundations\Pagination\CustomerPresenter($categories))->render() !!}好了,這樣在頁面應該可以看到分頁連結中含有"上一頁"和"下一頁"加數字的樣式了。

那麼如果擴展simplePaginate?其實很簡單,只要繼承剛才的CustomerPresenter ,實現hasPages 和render ,至於為什麼可以按照我上面查看源碼的方式看一下就知道了,比如我們改成"上一篇"和"下一篇"

新建App\Foundations\Pagination\CustomerSimplePresenter.php

rrreee

分頁顯示:

{!! with(new \App\Foundations\Pagination\CustomerSimplePresenter($categories))->render() !!}

方法就是這個方法,具體修改按照自己需求重寫其中對應的顯示html元素的方法就可以了。

相關推薦:

利用Laravel產生Gravatar頭像位址的優雅方法

Laravel重設密碼實例詳解

laravel編寫APP介面(API)

以上是Laravel使用Pagination外掛程式實現自訂分頁的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板