NadiaPaginatorBundle
NadiaPaginatorBundle can help you build pagination UI quickly. You can design your filter and search UI with Symfony Form component, and easily validate input parameters with Form., (*1)
, (*2)
Installation
Install with composer , run:, (*3)
composer require nadialabs/paginator-bundle
Register Bundle to AppKernel
// app/AppKernel.php
public function registerBundles()
{
return array(
// ...
new Nadia\Bundle\PaginatorBundle\NadiaPaginatorBundle(),
// ...
);
}
Bundle Configuration
nadia_paginator:
default_options:
input_key_class: Nadia\Bundle\PaginatorBundle\Input\InputKeys
default_page_size: 10
default_page_range: 8
session_enabled: false
default_translation_domain: null
templates:
pages: '@NadiaPaginator/templates/bootstrap4/pages.html.twig'
searches: '@NadiaPaginator/templates/bootstrap4/searches.html.twig'
filters: '@NadiaPaginator/templates/bootstrap4/filters.html.twig'
sorts: '@NadiaPaginator/templates/bootstrap4/sorts.html.twig'
sort_link: '@NadiaPaginator/templates/bootstrap4/sort_link.html.twig'
page_sizes: '@NadiaPaginator/templates/bootstrap4/page_sizes.html.twig'
- default_options
- input_key_class: The class that define the http request parameter keys.
- default_page_size: Default page size
- default_page_range: Default page range (control page link amounts)
- session_enabled: Enable session support, store input data in session.
You can use a clean URL without query string when enabled it.
Otherwise the filter, search, sort, page size, and page parameters will be part of URL query string, or transfer by POST request body.
- default_translation_domain: Default
translation-domain
value for each Form.
- templates
- pages: Template for rendering pages
- searches: Template for rendering searches block
- filters: Template for rendering filters block
- sorts: Template for rendering sort selection block
- sort_link: Template for rendering sort link
- page_sizes: Template for rendering page size selection block
Create PaginatorType class
Define a Paginator configuration., (*4)
Example class: AppBundle\Paginator\Movies\PaginatorType
, (*5)
You can implement these methods to define a PaginatorType class:, (*6)
buildSearch
Use SearchBuilder
to setup search form elements with Symfony FormType styles., (*7)
buildFilter
Use FilterBuilder
to setup filter form elements with Symfony FormType styles., (*8)
buildSort
Use SortBuilder
to setup sorting methods., (*9)
buildPageSize
Use PageSizeBuilder
to setup page size list., (*10)
Setup the paginator form options, return an array contain form options., (*11)
Setup the paginator options, you can define custom options to injecting extra data into PaginatorType. PaginatorType will use this options in these methods: buildSearch
buildFilter
buildSort
buildPageSize
getFormOptions
., (*12)
Create SearchQueryCompiler class
Define a QueryCompiler to build custom search criteria., (*13)
Example class for Doctrine QueryBuilder: AppBundle\Paginator\Movies\FilterQueryCompiler
, (*14)
Create FilterQueryCompiler class
Define a QueryCompiler to build custom filter criteria., (*15)
Example class for Doctrine QueryBuilder: AppBundle\Paginator\Movies\FilterQueryCompiler
, (*16)
Example controller: MovieController
Example entities: Movie
, People
, (*17)
To get a Pagination instance, the process as below:, (*18)
- Use
PaginatorTypeContainer
to generate a PaginatorType instance.
- Use
PaginatorFactory
to generate a Paginator instance with a PaginatorType instance.
- Create a query instance (in this example is a Doctrine ORM QueryBuilder instance)
- Do paginate with Paginator, and get a Pagination instance
- Render a view with Pagination instance
// Example controller action
$options = [
'movieCompanies' => [
'Warner Bros.',
'Sony Pictures',
'Walt Disney',
'Universal Pictures',
'20th Century Fox',
'Paramount Pictures',
'Lionsgate Films',
'The Weinstein Company',
'DreamWorks Pictures',
],
];
$paginator = $this->get('nadia_paginator.paginator_factory')->create(PaginatorType::class, $options);
$qb = $this->getDoctrine()->getRepository(Movie::class)
->createQueryBuilder('movie')
->select(['movie', 'director'])
->leftJoin('movie.director', 'director')
;
/** @var Movie[]|Collection|Pagination $pagination */
$pagination = $paginator->paginate($qb);
Rendering HTML with Twig
You can easily render a list page with this template @NadiaPaginator/templates/bootstrap4/pagination.html.twig
Use twig embed
tag to overwrite tableContent
block and put your table contents in tableContent
block., (*19)
Example twig file: index.html.twig
, (*20)
// Render view
$this->render('@App/Movie/index.html.twig', ['pagination' => $pagination]);
```twig
{# Example view file #}, (*21)
{% use '@NadiaPaginator/templates/bootstrap4/pagination.html.twig' %}, (*22)
{{ block('pagination') }}
{% block tableContent %}, (*23)
{{ nadia_paginator_sort_link(pagination, 'title', 'movie.title', 'ASC') }}
|
{{ nadia_paginator_sort_link(pagination, 'description', 'movie.description', 'ASC') }}
|
{{ nadia_paginator_sort_link(pagination, 'releasedAt', 'movie.releasedAt', 'DESC') }}
|
Director |
Company |
{% for movie in pagination %}
{{ movie.title }} |
{{ movie.description }} |
{{ movie.releasedAt|date('Y-m-d') }} |
{{ movie.director.name }} |
{{ movie.company }} |
{% endfor %}
{% endblock tableContent %}, (*24)
### Rendering searches block in Twig
Use `nadia_paginator_searches` method to render search block in Twig template.
```twig
{{ nadia_paginator_searches(pagination, {attributes: {class: 'col-md-6 col-xl-4 search'}}) }}
Rendering filters block in Twig
Use nadia_paginator_filters
method to render filter block in Twig template., (*25)
{{ nadia_paginator_filters(pagination, {attributes: {class: 'col-md-4 col-xl-3 mb-1 filter'}}) }}
Rendering sorts block in Twig
Use nadia_paginator_sorts
method to render sorting block in Twig template., (*26)
{{ nadia_paginator_sorts(pagination) }}
Rendering page sizes block in Twig
Use nadia_paginator_page_sizes
method to render page sizes block in Twig template., (*27)
{{ nadia_paginator_page_sizes(pagination) }}
Rendering sort link in Twig
{{ nadia_paginator_sort_link(pagination, 'title', 'movie.title', 'ASC') }}