內容目录上一个主题< 读取配置(Reading Configurations) 下一个主题 |
分页(Pagination)¶The process of pagination takes place when we need to present big groups of arbitrary data gradually. 数据适配器(Data Adapters)¶This component makes use of adapters to encapsulate different sources of data:
示例(Examples)¶In the example below, the paginator will use the result of a query from a model as its source data, and limit the displayed data to 10 records per page: <?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();
The <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>
The <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; ?>
适配器使用(Adapters Usage)¶An example of the source data that must be used for each adapter: <?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,
]
);
页面属性(Page Attributes)¶The
自定义适配器(Implementing your own adapters)¶The Phalcon\Paginator\AdapterInterface interface must be implemented in order to create your own paginator adapters or extend the existing ones: <?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();
}
|