====== Пэйджер ====== * [[http://knpbundles.com/search?q=pagination]] ===== KnpBundles ===== ==== Инсталяция ==== Нужно добавить в компосер { "require": { "knplabs/knp-paginator-bundle": "dev-master" } } Альтернативные способы [knp-components] git=http://github.com/KnpLabs/knp-components.git [KnpPaginatorBundle] git=http://github.com/KnpLabs/KnpPaginatorBundle.git target=bundles/Knp/Bundle/PaginatorBundle # Install Knp components git clone git://github.com/KnpLabs/knp-components.git vendor/knp-components # Install knp paginator bundle git clone git://github.com/KnpLabs/KnpPaginatorBundle.git vendor/bundles/Knp/Bundle/PaginatorBundle ==== Configuration example ==== You can configure default query parameter names and templates knp_paginator: page_range: 5 # default page range used in pagination control default_options: page_name: page # page query parameter name sort_field_name: sort # sort field query parameter name sort_direction_name: direction # sort direction query parameter name distinct: true # ensure distinct results, useful when ORM queries are using GROUP BY statements template: pagination: KnpPaginatorBundle:Pagination:sliding.html.twig # sliding pagination controls template sortable: KnpPaginatorBundle:Pagination:sortable_link.html.twig # sort link template ==== Add the namespaces to your autoloader unless you are using Composer ==== // File: app/autoload.php $loader->registerNamespaces(array( 'Knp\\Component' => __DIR__.'/../vendor/knp-components/src', 'Knp\\Bundle' => __DIR__.'/../vendor/bundles', // ... )); ==== Add PaginatorBundle to your application kernel ==== // app/AppKernel.php public function registerBundles() { return array( // ... new Knp\Bundle\PaginatorBundle\KnpPaginatorBundle(), // ... ); } Usage examples: ==== Controller ==== Currently paginator can paginate: Doctrine\ORM\Query Doctrine\ORM\QueryBuilder Doctrine\ODM\MongoDB\Query\Query Doctrine\ODM\MongoDB\Query\Builder Doctrine\Common\Collection\ArrayCollection - any doctrine relation collection including ModelCriteria - Propel ORM query array with Solarium_Client and Solarium_Query_Select as elements // Acme\MainBundle\Controller\ArticleController.php public function listAction() { $em = $this->get('doctrine.orm.entity_manager'); $dql = "SELECT a FROM AcmeMainBundle:Article a"; $query = $em->createQuery($dql); $paginator = $this->get('knp_paginator'); $pagination = $paginator->paginate( $query, $this->get('request')->query->get('page', 1)/*page number*/, 10/*limit per page*/ ); // parameters to template return $this->render('AcmeMainBundle:Article:list.html.twig', array('pagination' => $pagination)); } ==== View ==== {# total items count #}
{{ pagination.getTotalItemCount }}
{# sorting of properties based on query components #} {{ knp_pagination_sortable(pagination, 'Title', 'a.title') }} {# table body #} {% for article in pagination %} {% endfor %}
{{ knp_pagination_sortable(pagination, 'Id', 'a.id') }}
{{ article.id }} {{ article.title }}
{# display navigation #}
==== Translation in view ==== For translating the following text: * %foo% name with translation key table_header_name. The translation is in the domain messages. * {0} No author|{1} Author|[2,Inf] Authors with translation key table_header_author. The translation is in the domain messages. translationCount and translationParameters can be combined. {# sorting of properties based on query components #} {{ knp_pagination_sortable(pagination, 'Title', 'a.title')|raw }}
{{ knp_pagination_sortable(pagination, 'Id'|trans({foo:'bar'},'messages'), 'a.id' )|raw }} {{ knp_pagination_sortable(pagination, 'Author'|trans({}, 'messages'), 'a.author' )|raw }}
==== Dependency Injection ==== You can automatically inject a paginator service into another service by using the knp_paginator.injectable DIC tag. The tag takes one optional argument paginator, which is the ID of the paginator service that should be injected. It defaults to knp_paginator. The class that receives the KnpPaginator service must implement Knp\Bundle\PaginatorBundle\Definition\PaginatorAwareInterface. If you're too lazy you can also just extend the Knp\Bundle\PaginatorBundle\Definition\PaginatorAware base class. ==== Creating custom subscriber ====