2015-12-16 48 views
0

看看這個文件,但找不到我需要的確切答案,所以不知道是否有可能,但這是我想實現的:學說分頁 - 限制顯示的頁面數量?

我有一個客戶端列表分頁我的Symfony2項目。目前數據庫中有幾百個,但預計會增長。目前有22頁的客戶,我使用的分頁是標準的Doctrine2分頁。我在我的控制器,向客戶展示功能如下:

$em = $this->getDoctrine()->getManager(); 
$client_repo = $em->getRepository('AppBundle:Clients'); 
$clients = $client_repo->findByAccountStatus($status); 
$message = false; 
$page = $request->get('page'); 
$total_clients = count($clients); 
$per_page = 20; 
$total_pages = ceil($total_clients/$per_page); 

if (!is_numeric($page)) { 
    $page = 1; 
} else { 
    $page = floor($page); 
} 
if ($total_clients <= $per_page) { 
    $page = 1; 
} 
if (($page * $per_page) > $total_clients) { 
    $page = $total_pages; 
} 
$offset = 0; 
if ($page > 1) { 
    $offset = $per_page * ($page - 1); 
} 

$dql = "SELECT c FROM AppBundle:Clients c WHERE c.accountStatus = '".$status."'"; 
$query = $em->createQuery($dql) 
    ->setFirstResult($offset) 
    ->setMaxResults($per_page); 

$paginator = new Paginator($query, $fetchJoinCollection = false); 


return $this->render('AppBundle:tables:clientstable.html.twig', array(
    'clients' => $paginator, 
    'total_pages' => $total_pages, 
    'current_page' => $page 
)); 

我的樹枝文件按以下方式實現分頁:

{% if total_pages > 1 %} 
<div class="text-center"> 
    <ul class="pagination"> 
     {% for i in 1..total_pages %} 
      {% if loop.first %} 
       <li class="prev{% if current_page==1 %} disabled{% endif %}"><a href="{% if current_page>1 %}{{ path('app_show_clients', {'page':current_page-1}) }}{% else %}javascript:void(0){% endif %}">«</a></li> 
      {% endif %} 
       <li{% if current_page==loop.index %} class="active"{% endif %}><a href="{{ path('app_show_clients', {'page':loop.index}) }}">{{ loop.index }}</a></li> 
      {% if loop.last %} 
       <li class="next{% if current_page == total_pages %} disabled{% endif %}"><a href="{% if current_page < total_pages %}{{ path('app_show_clients', {'page':current_page+1}) }}{% else %}javascript:void(0){% endif %}">»</a></li> 
      {% endif %} 
     {% endfor %} 
    </ul> 
</div> 
{% endif %} 

目前,該分頁程序顯示所有網頁,但在幾個月的時間可能是現在的客戶數量的兩倍,頁面數量會在屏幕上溢出,看起來很亂。我想知道的是,是否有任何方法可以限制分頁區塊中任何時候顯示的頁面數量,例如,它顯示10個頁面,但是當您點擊一個頁面時,它會提供更多的內容,例如:

[< <] [1] [2] [3] ... [20] [21] [22] [>>]

然後當你點擊第3頁它可能看起來像這樣:

[< <] [3] [4] [5] ... [20] [21] [22] [>>]

或類似的東西?

希望我解釋得很好!

邁克爾

回答

0

您必須添加類似的東西在視圖:

{% for p in range(max(current_page-3, 1), min(current_page+3, total_pages)) %} 
    <a {% if p == current_page %} class="current-page"{% endif %} href="{{ path('route_name', {'page': p})) }}">{{ p }}</a> 
{% endfor %} 

希望它會幫助你。

0

不要讓自己複雜化,使用https://github.com/KnpLabs/KnpPaginatorBundle,非常容易使用,所有模板都是可配置的。已經與bootstrap 2和3集成在一起。如果你願意,你可以使用其他的,但這對我來說已經很多次了。