友善的 Symfony 分頁器可對所有內容進行分頁
一般來說,這個套件是基於 Knp Pager 元件的。該元件引入了一種不同的分頁處理方式。您可以在給定的文檔連結上閱讀有關內部邏輯的更多資訊。
注意:保持knp-components與此套件同步。如果您想使用舊版的 KnpPaginatorBundle - 在儲存庫中使用v3.0或v4.X標籤,適用於對ODM MongoDB和ORM 2.0查詢進行分頁
有關更改的詳細信息,請閱讀有關版本的信息。
>=4.4
。>=6.4
版本。>=3.0
版本。 注意:使用多個分頁器需要設定別名以保持參數不衝突。
composer require knplabs/knp-paginator-bundle
如果你不使用flex(你應該使用),你需要手動啟用bundle:
// app / AppKernel . php
public function registerBundles ()
{
return [
// ...
new Knp Bundle PaginatorBundle KnpPaginatorBundle (),
// ...
];
}
您可以配置預設查詢參數名稱和模板,以及一些其他選項:
knp_paginator :
convert_exception : false # throw a 404 exception when an invalid page is requested
page_range : 5 # number of links shown in the pagination menu (e.g: you have 10 pages, a page_range of 3, on the 5th page you'll see links to page 4, 5, 6)
remove_first_page_param : false # remove the page query parameter from the first page link
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
filter_field_name : filterField # filter field query parameter name
filter_value_name : filterValue # filter value query parameter name
page_out_of_range : ignore # ignore, fix, or throwException when the page is out of range
default_limit : 10 # default number of items per page
template :
pagination : ' @KnpPaginator/Pagination/sliding.html.twig ' # sliding pagination controls template
rel_links : ' @KnpPaginator/Pagination/rel_links.html.twig ' # <link rel=...> tags template
sortable : ' @KnpPaginator/Pagination/sortable_link.html.twig ' # sort link template
filtration : ' @KnpPaginator/Pagination/filtration.html.twig ' # filters template
// config / packages / paginator . php
<?php declare (strict_types= 1 );
use Symfony Component DependencyInjection Loader Configurator ContainerConfigurator ;
return static function ( ContainerConfigurator $ configurator ): void
{
$ configurator -> extension ( ' knp_paginator ' , [
' convert_exception ' => false , // throw a 404 exception when an invalid page is requested
' page_range ' => 5 , // number of links shown in the pagination menu ( e . g : you have 10 pages , a page_range of 3 , on the 5 th page you ' ll see links
' remove_first_page_param ' => false , // remove the page query parameter from the first page link
' 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
' filter_field_name ' => ' filterField ' , // filter field query parameter name
' filter_value_name ' => ' filterValue ' // filter value query parameter name
'page_out_of_range' => 'ignore', // ignore , fix , or throwException when the page is out of range
' default_limit ' => 10 // default number of items per page
],
' template ' => [
' pagination ' => ' @KnpPaginator/Pagination/sliding.html.twig ' , // sliding pagination controls template
' rel_links ' => ' @KnpPaginator/Pagination/rel_links.html.twig ' , // < link rel = ... > tags template
' sortable ' => ' @KnpPaginator/Pagination/sortable_link.html.twig ' , // sort link template
' filtration ' => ' @KnpPaginator/Pagination/filtration.html.twig ' // filters template
]
]);
};
這可以在knp_paginator.template.pagination
鍵中開箱即用:
@KnpPaginator/Pagination/sliding.html.twig
(預設)@KnpPaginator/Pagination/bootstrap_v5_pagination.html.twig
@KnpPaginator/Pagination/twitter_bootstrap_v4_pagination.html.twig
@KnpPaginator/Pagination/twitter_bootstrap_v3_pagination.html.twig
@KnpPaginator/Pagination/twitter_bootstrap_pagination.html.twig
@KnpPaginator/Pagination/foundation_v6_pagination.html.twig
@KnpPaginator/Pagination/foundation_v5_pagination.html.twig
@KnpPaginator/Pagination/bulma_pagination.html.twig
@KnpPaginator/Pagination/semantic_ui_pagination.html.twig
@KnpPaginator/Pagination/materialize_pagination.html.twig
@KnpPaginator/Pagination/tailwindcss_pagination.html.twig
@KnpPaginator/Pagination/uikit_v3_pagination.html.twig
這可以在knp_paginator.template.rel_links
鍵中開箱即用:
@KnpPaginator/Pagination/rel_links.html.twig
(預設) 這可以在knp_paginator.template.sortable
鍵中開箱即用:
@KnpPaginator/Pagination/sortable_link.html.twig
(預設)@KnpPaginator/Pagination/bootstrap_v5_bi_sortable_link.html.twig
@KnpPaginator/Pagination/bootstrap_v5_fa_sortable_link.html.twig
@KnpPaginator/Pagination/bootstrap_v5_md_sortable_link.html.twig
@KnpPaginator/Pagination/twitter_bootstrap_v3_sortable_link.html.twig
@KnpPaginator/Pagination/twitter_bootstrap_v4_font_awesome_sortable_link.html.twig
@KnpPaginator/Pagination/twitter_bootstrap_v4_material_design_icons_sortable_link.html.twig
@KnpPaginator/Pagination/semantic_ui_sortable_link.html.twig
@KnpPaginator/Pagination/uikit_v3_sortable.html.twig
這可以在knp_paginator.template.filtration
密鑰中開箱即用:
@KnpPaginator/Pagination/filtration.html.twig
(預設)@KnpPaginator/Pagination/bootstrap_v5_filtration.html.twig
@KnpPaginator/Pagination/twitter_bootstrap_v4_filtration.html.twig
目前分頁器可以分頁:
array
DoctrineDBALQueryQueryBuilder
DoctrineORMQuery
DoctrineORMQueryBuilder
DoctrineODMMongoDBQueryQuery
DoctrineODMMongoDBQueryBuilder
DoctrineODMPHPCRQueryQuery
DoctrineODMPHPCRQueryBuilderQueryBuilder
DoctrineCommonCollectionArrayCollection
- 任何 Doctrine 關係集合,包括ModelCriteria
- 推進 ORM 查詢Solarium_Client
和Solarium_Query_Select
作為元素的陣列 // App Controller ArticleController . php
public function listAction ( EntityManagerInterface $ em , PaginatorInterface $ paginator , Request $ request )
{
$ dql = " SELECT a FROM AcmeMainBundle:Article a " ;
$ query = $ em -> createQuery ( $ dql );
$ pagination = $ paginator -> paginate (
$ query , / * query NOT result * /
$ request -> query -> getInt ( ' page ' , 1 ), / * page number * /
10 / * limit per page * /
);
// parameters to template
return $ this -> render ( ' article/list.html.twig ' , [ ' pagination ' => $ pagination ]);
}
<head>
中: {# rel links for pagination #}
{{ knp_pagination_rel_links( pagination ) }}
<body>
中: {# total items count #}
< div class = " count " >
{{ pagination . getTotalItemCount }}
</ div >
< table >
< tr >
{# sorting of properties based on query components #}
< th >{{ knp_pagination_sortable( pagination , ' Id ' , ' a.id ' ) }}</ th >
< th {% if pagination. isSorted ( ' a.title ' ) %} class = " sorted " {% endif %}>
{{ knp_pagination_sortable( pagination , ' Title ' , ' a.title ' ) }}
</ th >
< th {% if pagination. isSorted ([ ' a.date ' , ' a.time ' ]) %} class = " sorted " {% endif %}>
{{ knp_pagination_sortable( pagination , ' Release ' , [ ' a.date ' , ' a.time ' ]) }}
</ th >
</ tr >
{# table body #}
{% for article in pagination %}
< tr {% if loop . index is odd %} class = " color " {% endif %}>
< td >{{ article . id }}</ td >
< td >{{ article . title }}</ td >
< td >{{ article . date | date ( ' Y-m-d ' ) }}, {{ article . time | date ( ' H:i:s ' ) }}</ td >
</ tr >
{% endfor %}
</ table >
{# display navigation #}
< div class = " navigation " >
{{ knp_pagination_render( pagination ) }}
</ div >
用於翻譯以下文字:
%foo% name
,帶有翻譯鍵table_header_name
。翻譯是在域messages
中。{0} No author|{1} Author|[2,Inf] Authors
具有翻譯鍵table_header_author
的作者。翻譯是在域messages
中。TranslationCount 和 TranslationParameters 可以組合。
< table >
< tr >
{# sorting of properties based on query components #}
< th >{{ knp_pagination_sortable( pagination , ' Id ' | trans({ foo : ' bar ' }, ' messages ' ), ' a.id ' ) | raw }}</ th >
< th {% if pagination. isSorted ( ' a.Title ' ) %} class = " sorted " {% endif %}>{{ knp_pagination_sortable( pagination , ' Title ' , ' a.title ' ) | raw }}</ th >
< th >{{ knp_pagination_sortable( pagination , ' Author ' | trans({}, ' messages ' ), ' a.author ' ) | raw }}</ th >
</ tr >
<!-- Content of the table -->
</ table >
您也可以透過以下名稱格式建立翻譯檔案來覆蓋翻譯: domain.locale.format
。因此,要為此套件建立翻譯文件,您需要在project_root/translations/
下建立例如KnpPaginatorBundle.tr.yaml
文件,並在其中新增翻譯:
label_previous : " Önceki "
label_next : " Sonraki "
filter_searchword : " Arama kelimesi "
如果您相應地設定配置的預設翻譯:
framework :
default_locale : tr
Symfony 會自動選擇它。
您可以使用knp_paginator.injectable
DIC 標記將分頁器服務自動注入到另一個服務。此標籤採用一個可選參數paginator
,它是應注入的分頁器服務的 ID。它預設為knp_paginator
。
接收 KnpPaginator 服務的類別必須實作KnpBundlePaginatorBundleDefinitionPaginatorAwareInterface
。如果你太懶了,你也可以擴充KnpBundlePaginatorBundleDefinitionPaginatorAware
基底類別。
不鼓勵使用
PaginatorAwareInterface
發出警告,並且可能會在未來版本中刪除。您不應該依賴 setter 注入,而只能依賴正確的建構函式註入。使用 Symfony 內建的自動組裝機制是建議的方法。
如果安裝了symfony/proxy-manager-bridge
包, knp_paginator
服務將被延遲建立。
有關惰性服務的更多信息,請參閱有關依賴注入的 Symfony 文件。
<? xml version = " 1.0 " ?>
< container xmlns = " http://symfony.com/schema/dic/services "
xmlns : xsi = " http://www.w3.org/2001/XMLSchema-instance "
xsi : schemaLocation = " http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd " >
< parameters >
< parameter key = " my_bundle.paginator_aware.class " >MyBundleRepositoryPaginatorAwareRepository</ parameter >
</ parameters >
< services >
< service id = " my_bundle.paginator_aware " class = " my_bundle.paginator_aware.class " >
< tag name = " knp_paginator.injectable " paginator = " knp_paginator " />
</ service >
</ services >
</ container >
framework :
translator : { fallbacks: ['%locale%'] }
translations/KnpPaginatorBundle.en.yml
中建立自己的翻譯檔案(如果需要,請將「en」替換為您自己的語言程式碼)。然後加入這些行: label_next : Next
label_previous : Previous
<rel>
連結僅在使用分頁時才有意義,它們與排序或過濾無關。 請先閱讀這篇文章。
該庫由以下人員維護(按字母順序排列):