bajzany / paginator
Paginator for Nette Framework
Installs: 600
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:extensions
Requires
- php: ^7.2
- chomenko/route-listener: ^1.0
- doctrine/orm: ^2.6
- latte/latte: ^2.4
- nette/application: ^2.4
- nette/bootstrap: ^2.4
- nette/di: ^2.4
- nette/utils: ^2.4
This package is auto-updated.
Last update: 2024-10-25 08:25:17 UTC
README
Paginator
Nette paginator for using doctrine query
Required:
Installation
-
Composer installation
composer require bajzany/paginator dev-master
-
Register into Nette Application
extensions: BajzanyPaginator: Bajzany\Paginator\DI\PaginationExtension
How to use into another component
For example i show you this component in bajzany/table
Register paginatorControl into constructor
/**
* @var IPaginationControl
*/
private $paginationControl;
public function __construct(ITable $table, IPaginationControl $paginationControl, $name = NULL)
{
parent::__construct($name);
$this->table = $table;
$this->paginationControl = $paginationControl;
}
Now into same file create functions renderPaginator, createComponentPaginator and getPaginationControl
public function renderPaginator()
{
$paginatorComponent = $this->getComponent(self::PAGINATOR_NAME);
$paginatorComponent->render();
}
/**
* @return \Bajzany\Paginator\PaginationControl
* @throws TableException
*/
public function createComponentPaginator()
{
$paginator = $this->table->getPaginator();
if (empty($paginator)) {
throw TableException::paginatorIsNotSet(get_class($this->table));
}
return $this->getPaginationControl()->create($paginator);
}
/**
* @return IPaginationControl
*/
public function getPaginationControl(): IPaginationControl
{
return $this->paginationControl;
}
Next point is into EntityTable.php
Constructor create new instance QueryPaginator() and save this into paginator property
/**
* @param string $entityClass
* @param EntityManager $entityManager
*/
public function __construct(string $entityClass, EntityManager $entityManager)
{
parent::__construct();
$this->entityClass = $entityClass;
$this->entityManager = $entityManager;
$this->entityRepository = $this->getEntityManager()->getRepository($this->getEntityClass());
$this->queryBuilder = $this->entityRepository->createQueryBuilder('e');
$this->paginator = new QueryPaginator();
}
Important is function build into EntityTable.php
Here is queryBuilder which have function getQuery() necessarily for function $paginator->setQuery($query) Paginator return updated Query and create pagination items (1,2,3, ...)
/**
* @param IContainer $container
* @return ITable
*/
public function build(IContainer $container): ITable
{
if ($this->isBuild()) {
return $this;
}
$this->queryBuilder->whereCriteria($this->getWhere());
foreach ($this->getSort() as $by => $sort) {
$this->queryBuilder->addOrderBy($by, $sort);
}
$query = $this->queryBuilder->getQuery();
$paginator = $this->getPaginator();
if ($paginator instanceof QueryPaginator) {
$paginator->setQuery($query);
$query = $paginator->getQuery();
// IT'S BECAUSE ATTACH FUNCTION IN TABLECONTROL
$container->getComponent(TableControl::PAGINATOR_NAME);
}
$this->entities = $query->getResult();
return parent::build($container);
}
In latte for rendering pagination please use control nameYourComponent:paginator
<div class="box-footer clearfix">
{control tableEntity:paginator}
</div>
In Presenter where i have function createComponentTableEntity you can change paginator pageSize, add another items into list pagination...
public function createComponentTableEntity()
{
$table = $this->tableFactory->createEntityTable(User::class);
$paginator = $table->getPaginator();
$item = $paginator->getPaginatorWrapped()->createItem();
$item->getContent()->addHtml(Html::el('')->setText('-1 Léňa'));
$table->getPaginator()->setPageSize(2);
...
...
...
return $this->tableFactory->createComponentTable($table);
}
PaginatiorWrapped is nette Html object it's very easy edit it or adding another children