如何在symfony2中实现分页?

时间:2015-07-17 02:16:07

标签: php symfony

我正在使用Symfony2框架开展项目。我正在使用XAMPP与PHP版本5.5.19。我有桌子,我想做的是为了轻松导航等等...我是symfony的新手,我不知道如何实现它?

有人能帮助我吗?

4 个答案:

答案 0 :(得分:7)

Doctrine ORM(包含在Symfony标准版中)自2.2以来可用builtin paginator

它的创建是为了避免第三方分页器的扩散(如前面提到过的PagerFanta和KnpPaginator),现在被这些分页器使用并且基于它们的代码。

使用起来非常简单,不需要任何外部依赖:

// Extracted from the Doctrine doc

use Doctrine\ORM\Tools\Pagination\Paginator;

$dql = "SELECT p, c FROM BlogPost p JOIN p.comments c";
$query = $entityManager->createQuery($dql)
    ->setFirstResult(0)
    ->setMaxResults(100);

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

答案 1 :(得分:3)

在我的项目中,我使用并推荐Pagerfanta包,维护和无错分页包。

答案 2 :(得分:0)

// Twig文件代码

<nav>
        <ul class="pagination">
            <li {% if page_no <= 1 %} class='disabled' {% endif %}>
            <a {% if page_no > 1 %} href='{{ path('pagination', {'page':previous_page}) }}' {% endif %}>Previous</a> 
            </li>

        {% if total_no_of_pages <= 10 %}  
            {% set counter = 1 %}    
            {% for counter in counter..total_no_of_pages %}
                {% if counter == page_no %}
               <li class='active'><a>{{ counter }}</a></li> 
                {% else %}
               <li><a href='{{ path('pagination', {'page':counter}) }}'>{{ counter }}</a></li>
                {% endif %}
            {% set counter = counter + 1 %}
            {% endfor %}
        {% endif %}

        {% if total_no_of_pages > 10 %}         
        {% if page_no <= 4 %}
            {% for counter in 1..7 %} 
                {% if counter == page_no %}
               <li class='active'><a>{{ counter }}</a></li> 
                {% else %}
               <li><a href='{{ path('pagination', {'page':counter}) }}'>{{ counter }}</a></li>
                {% endif %}
            {% set counter = counter + 1 %}
            {% endfor %}
            <li><a>...</a></li>
            <li><a href='{{ path('pagination', {'page': second_last }) }}'>{{ second_last }}</a></li>
            <li><a href='{{ path('pagination', {'page': total_no_of_pages }) }}'>{{ total_no_of_pages }}</a></li>
        {% endif %}

        {% if ( page_no > 4 ) and ( page_no < total_no_of_pages - 4 ) %}
            {% set counterMinus = page_no - adjacents %}
            {% set counterPlus = page_no + adjacents %}
            <li><a href='{{ path('pagination', {'page': 1 }) }}'>1</a></li>
            <li><a href='{{ path('pagination', {'page': 2 }) }}'>2</a></li>
            <li><a>...</a></li>
            {% for counters in counterMinus..counterPlus %}         
               {% if counters == page_no %}
               <li class='active'><a>{{ counters }}</a></li>    
                {% else %}
               <li><a href='{{ path('pagination', {'page': counters }) }}'>{{ counters }}</a></li>
                {% endif %}
                {% set counters = counters + 1 %}                  
           {% endfor %}
           <li><a>...</a></li>
           <li><a href='{{ path('pagination', {'page': second_last }) }}'>{{ second_last }}</a></li>
           <li><a href='{{ path('pagination', {'page': total_no_of_pages }) }}'>{{ total_no_of_pages }}</a></li>      
        {% endif %}

            {% else %}
            <li><a href='{{ path('pagination', {'page': 1 }) }}'>1</a></li>
            <li><a href='{{ path('pagination', {'page': 2 }) }}'>2</a></li>
            <li><a>...</a></li>

            {% set counterMin = total_no_of_pages - 6 %}
            {% for counterss in counterMin..total_no_of_pages %}
                {% if counterss == page_no %}
               <li class='active'><a>{{ counterss }}</a></li>
                {% else %}
               <li><a href='{{ path('pagination', {'page': counterss }) }}'>{{ counterss }}</a></li>
                {% endif %} 
                {% set counterss = counterss + 1 %}              
            {% endfor %}
        {% endif %}

        {% if page_no >= total_no_of_pages - 4 %}
            <li><a href='{{ path('pagination', {'page': 1 }) }}'>1</a></li>
            <li><a href='{{ path('pagination', {'page': 2 }) }}'>2</a></li>
            <li><a>...</a></li>

            {% set lastCount = page_no %}
                {% for lastCount in page_no..total_no_of_pages %}
                {% if lastCount == page_no %}
                <li class='active'><a>{{ lastCount }}</a></li>
                {% else %}
               <li><a href='{{ path('pagination', {'page': lastCount }) }}'>{{ lastCount }}</a></li>
               {% endif %}
                {% set lastCount = lastCount + 1 %}
                {% endfor %}
        {% endif%}

        <li {% if page_no >= total_no_of_pages %} class='disabled' {% endif %}>
        <a {% if page_no < total_no_of_pages %} href='{{ path('pagination', {'page': next_page }) }}' {% endif %}>Next</a>
        </li>
        {% if page_no < total_no_of_pages %}
            <li><a href='{{ path('pagination', {'page': total_no_of_pages }) }}'>Last &rsaquo;&rsaquo;</a></li>
        {% endif %}
        </ul>
        </nav>

答案 3 :(得分:0)

//控制器文件

public function paginationAction($page)
{
    $commObj = new CommonController();
    $commObj->setContainer($this->container);
    /** session manager creation */
    $session = $this->getRequest()->getSession(); 
    /* Session Check */
    if(!$session->has('Id'))
    {
      return $this->redirectToRoute('login_login_homepage');   
    }

    if(isset($page) && (!empty($page))){
      $page_no = $page;
    }
    else{
      $page_no = 1;
    }

    $total_records_per_page = 25;
    $offset = ($page_no-1) * $total_records_per_page;
    $previous_page = $page_no - 1;
    $next_page = $page_no + 1;
    $adjacents = "2"; 

    $total_records = sizeof($lists);

    $total_no_of_pages = ceil($total_records / $total_records_per_page);
    $second_last = $total_no_of_pages - 1;

    $em = $this->getDoctrine()->getManager();

    $repository = $em->getRepository('AppAppBundle:MasterCustomer');
    $select_query       = $repository->createQueryBuilder('QB')
                                     ->select('QB.id,QB.uniqueId,QB.customerName,QB.emailId,QB.mobileNo,QB.customerAddress,QB.customerCity,QB.customerState,QB.customerCountry,QB.status')
                                     ->setFirstResult($offset)
                                     ->setMaxResults($total_records_per_page)
                                     ->where('QB.status!=:sts') 
                                     ->setParameter('sts', 2)
                                     ->orderBy('QB.id', 'DESC')
                                     ->getQuery();
    $list        = $select_query->getArrayResult();

    return $this->render('MasterMasterBundle:Customer:index.html.twig',array('lists' => $list, 'total_no_of_pages' => $total_no_of_pages, 'page_no' => $page_no, 'second_last' => $second_last, 'adjacents' => $adjacents, 'total_records' => $total_records, 'next_page' => $next_page, 'previous_page' => $previous_page, 'total_records_per_page' => $total_records_per_page));
}