⚠️ No longer under maintenance
This bundle is no longer under maintenance on today. Please use TtskchPaginatorBundle instead., (*1)
, (*2)
Most easy and customizable way to use Pagerfanta with Symfony., (*3)
Features
Advantages compared to WhiteOctoberPagerfantaBundle:, (*4)
- So light weight
- Customizable twig-templated views
-
Sortable link feature
- Easy to use with search form
- Preset bootstrap4 theme
Demo
You can easily try demo app like below on demo branch., (*5)
, (*6)
Requirement
Installation
$ composer require ttskch/pagerfanta-bundle
// config/bundles.php
return [
// ...
Ttskch\PagerfantaBundle\TtskchPagerfantaBundle::class => ['all' => true],
];
Usage
// FooController.php
public function index(FooRepository $fooRepository, Context $context)
{
$context->initialize('id');
$queryBuilder = $fooRepository
->createQueryBuilder('f')
->orderBy(sprintf('f.%s', $context->criteria->sort), $context->criteria->direction)
;
$adapter = new DoctrineORMAdapter($queryBuilder);
$pagerfanta = new Pagerfanta($adapter);
$pagerfanta
->setMaxPerPage($context->criteria->limit)
->setCurrentPage($context->criteria->page)
;
return $this->render('index.html.twig', [
'pagerfanta' => $pagerfanta,
]);
}
{# index.html.twig #}
{% set keys = ['id', 'name', 'email'] %}
{% for key in keys %}
{{ ttskch_pagerfanta_sortable(key) }} |
{% endfor %}
{% for item in pagerfanta.getCurrentPageResults() %}
{% for key in keys %}
{{ attribute(item, key) }} |
{% endfor %}
{% endfor %}
{{ ttskch_pagerfanta_pager(pagerfanta) }}
See src/Twig/PagerfantaExtension.php to learn more about twig functions., (*7)
Sort with property of joined entity
// FooController.php
// ...
$queryBuilder = $fooRepository
->createQueryBuilder('f')
->leftJoin('f.parent', 'p')
;
if (preg_match('/^parent\.(.+)$/', $context->criteria->sort, $m)) {
$sort = sprintf('p.%s', $m[1]);
} else {
$sort = sprintf('f.%s', $context->criteria->sort);
}
$queryBuilder->orderBy($sort, $context->criteria->direction);
// ...
{# index.html.twig #}
{# ... #}
<th>{{ ttskch_pagerfanta_sortable(id) }}</th>
<th>{{ ttskch_pagerfanta_sortable(name) }}</th>
<th>{{ ttskch_pagerfanta_sortable(email) }}</th>
<th>{{ ttskch_pagerfanta_sortable(parent.id) }}</th>
{# ... #}
Configuring
$ bin/console config:dump-reference ttskch_pagerfanta
# Default configuration for extension with alias: "ttskch_pagerfanta"
ttskch_pagerfanta:
page:
name: page
range: 5
limit:
name: limit
default: 10
sort:
key:
name: sort
direction:
name: direction
# "asc" or "desc"
default: asc
template:
pager: '@TtskchPagerfanta/pager/default.html.twig'
sortable: '@TtskchPagerfanta/sortable/default.html.twig'
Customizing views
Use preset bootstrap4 theme
Just configure bundle like below., (*8)
# config/packages/ttskch_pagerfanta.yaml
ttskch_pagerfanta:
template:
pager: '@TtskchPagerfanta/pager/bootstrap4.html.twig'
Use your own theme
Create your own templates and configure bundle like below., (*9)
# config/packages/ttskch_pagerfanta.yaml
ttskch_pagerfanta:
template:
pager: 'your/own/pager.html.twig'
sortable: 'your/own/sortable.html.twig'
// FooCriteria.php
use Ttskch\PagerfantaBundle\Entity\Criteria;
class FooCriteria extends Criteria
{
public $query;
}
// FooSearchType.php
use Symfony\Component\Form\Extension\Core\Type\SearchType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Ttskch\PagerfantaBundle\Form\CriteriaType;
class FooSearchType extends CriteriaType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
$builder
->add('query', SearchType::class)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => FooCriteria::class,
// if your app depends on symfony/security-csrf adding below is recommended
// 'csrf_protection' => false,
]);
}
}
// FooRepository.php
public function createQueryBuilderFromCriteria(FooCriteria $criteria)
{
return $this->createQueryBuilder('f')
->where('f.name like :query')
->orWhere('f.email like :query')
->setParameter('query', sprintf('%%%s%%', str_replace('%', '\%', $criteria->query)))
->orderBy(sprintf('f.%s', $criteria->sort), $criteria->direction)
;
}
// FooController.php
public function index(FooRepository $fooRepository, Context $context)
{
$context->initialize('id', FooCriteria::class, FooSearchType::class);
$queryBuilder = $fooRepository->createQueryBuilderFromCriteria($context->criteria);
$adapter = new DoctrineORMAdapter($queryBuilder);
$pagerfanta = new Pagerfanta($adapter);
$pagerfanta
->setMaxPerPage($context->criteria->limit)
->setCurrentPage($context->criteria->page)
;
return $this->render('index.html.twig', [
'form' => $context->form->createView(),
'pagerfanta' => $pagerfanta,
]);
}
{# index.html.twig #}
{{ form(form, {action: path('index'), method: 'get'}) }}
{% set keys = ['id', 'name', 'email'] %}
{% for key in keys %}
{{ ttskch_pagerfanta_sortable(key) }} |
{% endfor %}
{% for item in pagerfanta.getCurrentPageResults() %}
{% for key in keys %}
{{ attribute(item, key) }} |
{% endfor %}
{% endfor %}
{{ ttskch_pagerfanta_pager(pagerfanta) }}