Errorhere
Errorhere

Reputation: 495

How can I implement pagination in symfony2?

I am working on a project using Symfony2 framework. I am using XAMPP with PHP version 5.5.19. I have table and what I want to do is to put pagination for easy navigation and so on... I am new to symfony that I don't know how to implement it?

Can somebody help me?

Upvotes: 1

Views: 4593

Answers (4)

user11577542
user11577542

Reputation: 1

//Twig file code

<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>

Upvotes: 0

user11577542
user11577542

Reputation: 1

// controller file

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));
}

Upvotes: 0

K&#233;vin Dunglas
K&#233;vin Dunglas

Reputation: 3024

Doctrine ORM (included in the Symfony standard edition) has a builtin paginator available since 2.2.

It has been created to avoid the proliferation of third party paginators (such as mentioned previously PagerFanta and KnpPaginator), is now used by those paginators and is based on their code.

It is very straightforward to use and does not require any external dependency:

// 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);

Upvotes: 6

Alexandru Olaru
Alexandru Olaru

Reputation: 7092

In my projects I use and recommend the Pagerfanta bundle, maintained, and bug free pagination bundle.

Upvotes: 3

Related Questions