ttskch/pagerfanta-bundle

This package is abandoned and no longer maintained. No replacement package was suggested.

Most easy and customizable way to use Pagerfanta with Symfony

Installs: 466

Dependents: 0

Suggesters: 0

Security: 0

Stars: 3

Watchers: 1

Forks: 0

Open Issues: 0

Type:symfony-bundle

1.1.1 2020-01-31 05:27 UTC

This package is auto-updated.

Last update: 2020-08-24 15:52:40 UTC


README

This bundle is no longer under maintenance on today. Please use TtskchPaginatorBundle instead.

TtskchPagerfantaBundle

Travis (.com) Latest Stable Version Total Downloads

Most easy and customizable way to use Pagerfanta with Symfony.

Features

Advantages compared to WhiteOctoberPagerfantaBundle:

  • 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.

Requirement

  • PHP ^7.1.3
  • Symfony ^4.0

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'] %}
<table>
    <thead>
    <tr>
        {% for key in keys %}
            <th>{{ ttskch_pagerfanta_sortable(key) }}</th>
        {% endfor %}
    </tr>
    </thead>
    <tbody>
    {% for item in pagerfanta.getCurrentPageResults() %}
        <tr>
            {% for key in keys %}
                <td>{{ attribute(item, key) }}</td>
            {% endfor %}
        </tr>
    {% endfor %}
    </tbody>
</table>

{{ ttskch_pagerfanta_pager(pagerfanta) }}

See src/Twig/PagerfantaExtension.php to learn more about twig functions.

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.

# 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.

# config/packages/ttskch_pagerfanta.yaml

ttskch_pagerfanta:
    template:
        pager: 'your/own/pager.html.twig'
        sortable: 'your/own/sortable.html.twig'

Using with search form

// 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'] %}
<table>
    <thead>
    <tr>
        {% for key in keys %}
            <th>{{ ttskch_pagerfanta_sortable(key) }}</th>
        {% endfor %}
    </tr>
    </thead>
    <tbody>
    {% for item in pagerfanta.getCurrentPageResults() %}
        <tr>
            {% for key in keys %}
                <td>{{ attribute(item, key) }}</td>
            {% endfor %}
        </tr>
    {% endfor %}
    </tbody>
</table>

{{ ttskch_pagerfanta_pager(pagerfanta) }}