[TOC]
# 分頁
當我們需要逐漸呈現大量任意數據時,就會發生分頁過程。`Phalcon\Paginator`提供了一種快速便捷的方法,可將這些數據集拆分為可瀏覽頁面。
## 數據適配器
該組件使用適配器封裝不同的數據源:
| 適配器 | 描述 |
| ------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `Phalcon\Paginator\Adapter\NativeArray` | 使用PHP數組作為源數據 |
| `Phalcon\Paginator\Adapter\Model` | 使用`Phalcon\Mvc\Model\Resultset`對象作為源數據。由于PDO不支持可滾動游標,因此不應使用此適配器對大量記錄進行分頁 |
| `Phalcon\Paginator\Adapter\QueryBuilder` | 使用`Phalcon\Mvc\Model\Query\Builder`對象作為源數據|
## 工廠
使用`adapter`選項加載Paginator Adapter類
```php
<?php
use Phalcon\Paginator\Factory;
$builder = $this->modelsManager->createBuilder()
->columns('id, name')
->from('Robots')
->orderBy('name');
$options = [
'builder' => $builder,
'limit' => 20,
'page' => 1,
'adapter' => 'queryBuilder',
];
$paginator = Factory::load($options);
```
## 示例
在下面的示例中,分頁器將使用模型中的查詢結果作為其源數據,并將顯示的數據限制為每頁10條記錄:
```php
<?php
use Phalcon\Paginator\Adapter\Model as PaginatorModel;
// Current page to show
// In a controller/component this can be:
// $this->request->getQuery('page', 'int'); // GET
// $this->request->getPost('page', 'int'); // POST
$currentPage = (int) $_GET['page'];
// The data set to paginate
$robots = Robots::find();
// Create a Model paginator, show 10 rows by page starting from $currentPage
$paginator = new PaginatorModel(
[
'data' => $robots,
'limit' => 10,
'page' => $currentPage,
]
);
// Get the paginated results
$page = $paginator->getPaginate();
```
`$currentPage` 變量控制要顯示的頁面。`$paginator->getPaginate()` 返回包含分頁數據的`$page`對象。它可以用于生成分頁:
```php
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Type</th>
</tr>
<?php foreach ($page->items as $item) { ?>
<tr>
<td><?php echo $item->id; ?></td>
<td><?php echo $item->name; ?></td>
<td><?php echo $item->type; ?></td>
</tr>
<?php } ?>
</table>
```
`$page` 對象還包含導航數據:
```php
<a href='/robots/search'>First</a>
<a href='/robots/search?page=<?= $page->before; ?>'>Previous</a>
<a href='/robots/search?page=<?= $page->next; ?>'>Next</a>
<a href='/robots/search?page=<?= $page->last; ?>'>Last</a>
<?php echo 'You are in page ', $page->current, ' of ', $page->total_pages; ?>
```
## 使用適配器
每個適配器必須使用的源數據示例:
```php
<?php
use Phalcon\Paginator\Adapter\Model as PaginatorModel;
use Phalcon\Paginator\Adapter\NativeArray as PaginatorArray;
use Phalcon\Paginator\Adapter\QueryBuilder as PaginatorQueryBuilder;
// Passing a resultset as data
$paginator = new PaginatorModel(
[
'data' => Products::find(),
'limit' => 10,
'page' => $currentPage,
]
);
// Passing an array as data
$paginator = new PaginatorArray(
[
'data' => [
['id' => 1, 'name' => 'Artichoke'],
['id' => 2, 'name' => 'Carrots'],
['id' => 3, 'name' => 'Beet'],
['id' => 4, 'name' => 'Lettuce'],
['id' => 5, 'name' => ''],
],
'limit' => 2,
'page' => $currentPage,
]
);
// Passing a QueryBuilder as data
$builder = $this->modelsManager->createBuilder()
->columns('id, name')
->from('Robots')
->orderBy('name');
$paginator = new PaginatorQueryBuilder(
[
'builder' => $builder,
'limit' => 20,
'page' => 1,
]
);
```
## 頁面屬性
The `$page` 對象具有以下屬性:
| 屬性 | 描述 |
| ------------- | ------------------------------------------------------ |
| `items` | 要在當前頁面顯示的記錄集 |
| `current` | 當前頁面 |
| `before` | 當前的上一頁 |
| `next` | 當前的下一頁 |
| `last` | 記錄集中的最后一頁 |
| `total_pages` | 頁數 |
| `total_items` | 源數據中的項目數 |
## 實現自己的適配器
必須實現 `Phalcon\Paginator\AdapterInterface` 接口才能創建自己的分頁器適配器或擴展現有的分頁器適配器:
```php
<?php
use Phalcon\Paginator\AdapterInterface as PaginatorInterface;
class MyPaginator implements PaginatorInterface
{
/**
* Adapter constructor
*
* @param array $config
*/
public function __construct($config);
/**
* Set the current page number
*
* @param int $page
*/
public function setCurrentPage($page);
/**
* Returns a slice of the resultset to show in the pagination
*
* @return stdClass
*/
public function getPaginate();
}
```
- 常規
- Welcome
- 貢獻
- 生成回溯
- 測試重現
- 單元測試
- 入門
- 安裝
- Web服務器設置
- WAMP
- XAMPP
- 教程
- 基礎教程
- 教程:創建一個簡單的REST API
- 教程:V?kuró
- 提升性能
- 教程:INVO
- 開發環境
- Phalcon Compose (Docker)
- Nanobox
- Phalcon Box (Vagrant)
- 開發工具
- Phalcon開發者工具的安裝
- Phalcon開發者工具的使用
- 調試應用程序
- 核心
- MVC應用
- 微應用
- 創建命令行(CLI)應用程序
- 依賴注入與服務定位
- MVC架構
- 服務
- 使用緩存提高性能
- 讀取配置
- 上下文轉義
- 類加載器
- 使用命名空間
- 日志
- 隊列
- 數據庫
- 數據庫抽象層
- Phalcon查詢語言(PHQL)
- ODM(對象文檔映射器)
- 使用模型
- 模型行為
- ORM緩存
- 模型事件
- 模型元數據
- 模型關系
- 模型事務
- 驗證模型
- 數據庫遷移
- 分頁
- 前端
- Assets管理
- 閃存消息
- 表單
- 圖像
- 視圖助手(標簽)
- 使用視圖
- Volt:模板引擎
- 業務邏輯
- 訪問控制列表(ACL)
- 注解解析器
- 控制器
- 調度控制器
- 事件管理器
- 過濾與清理
- 路由
- 在session中存儲數據
- 生成URL和路徑
- 驗證
- HTTP
- Cookies管理
- 請求環境
- 返回響應
- 安全
- 加密/解密
- 安全
- 國際化
- 國際化
- 多語言支持