1. 程式人生 > 其它 >bootstrap 顯示資料條數_使用陣列建立分頁資料

bootstrap 顯示資料條數_使用陣列建立分頁資料

技術標籤:bootstrap 顯示資料條數

73d6a7465c0c13e85bdf881500501427.png

php中文網最新課程

每日17點準時技術乾貨分享

bb7a5abda6ff50b316faccff7cde5eb0.png

ba91a48315b65f97aacaaa08e605b7de.gif

說明

使用版本:5.1.35 LTS

遇到的一個場景是,資料是從 RPC 遠端呼叫介面獲取的陣列,需要在前端頁面顯示分頁。

解決方法

可以使用 think\Paginate 類的 make 方法建立分頁資料。make 方法原型:

/**     * @access public     * @param       $items   需要分頁的資料     * @param       $listRows 每頁資料條數     * @param null  $currentPage 當前頁數     * @param null  $total  總頁數     * @param bool  $simple  是否使用簡單模式(只有上一頁和下一頁)     * @param array $options 其他引數選項,如查詢引數,url路徑等     * @return Paginator  返回一個分頁物件     */    public static function make($items, $listRows, $currentPage = null, $total = null, $simple = false, $options = []){        return new static($items, $listRows, $currentPage, $total, $simple, $options);    }

需要傳入的引數說明見程式碼註釋。

由於 think\Paginate 類的是一個抽象類,需要另一個類繼承它才能使用它的公有方法。框架中有 think\paginator\driver\Bootstrap 類繼承了它,所以可以使用該類去呼叫 make 方法。

所以,可以寫一個從陣列建立分頁資料的方法,大概是這樣的:

private function getPaginateData($data, $page, $query){    return Bootstrap::make($data, $perPage, $page, $total, false, ['path' => url('module/controller/action'), 'query' => $query]);}

使用該方法生成分頁物件後,比如:

$data = $this->getPaginateData(...)

在控制器中輸出到模板,然後就可以在模板頁面中新增:

{$data|raw}

模板引擎會自動渲染分頁樣式。

-END-

宣告:本文選自「php中文網」,搜尋「 phpcnnew 」即可關注!