pkshetlie/pagination-bundle

轻松实现带有分页的HTML表格

v2.0.14 2024-01-22 21:06 UTC

README

composer require pkshetlie/pagination-bundle

添加到 AppKernel.php

[ 
       ...
       new Pkshetlie\PaginationBundle\PaginationBundle(), 
       ... 
]

添加到 config.yml

imports:
  ...
  - { resource: "@PaginationBundle/Resources/config/services.yml" }

安装完成。

示例用法

在某个 Dummy 控制器中

class DummyController extends Controller{

   public function indexAction(Request $request){
       $qb = $this->getDoctrine()->getReposiitory('DummyBundle:DummyEntity')->createQueryBuilder('x');
       
       // you can add some 
       /*
       $qb->orderBy( ... )
       $qb->where( ... )
       ...
       */        
       
       $pagination = $this->get('pkshetlie.pagination')->process($qb, $request);
       
       return $this->render('DummyBundle:Dummy:index.html.twig',[
       'pagination'=> $pagination,
       ]);
   }

}

在 index.html.twig 中

{% import '@Pagination/Pagination/macro.twig' as macro_pagination %}

<table>
{% for entity in pagination.entities %}
   {# ... your stuff with <tr> / <td> #}
{% endfor %}
</table>

{# draw the pagination #}
{{ macro_pagination.paginate(pagination) }}

这就完了!