Thursday, June 6, 2013

Paginator Bundle :: KnpPaginatorBundle

1. Installation
    -add to composer.json

{
    "require": {
        "knplabs/knp-paginator-bundle": "dev-master"
    }
}
 
 
2. configure
  - add app/config/config.yml
 
knp_paginator:
    page_range: 5                      # default page range used in pagination control
    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
    template:
        pagination: KnpPaginatorBundle:Pagination:sliding.html.twig     # sliding pagination controls template
        sortable: KnpPaginatorBundle:Pagination:sortable_link.html.twig # sort link template
 
 
  3. Add to application kernel
   - add  app/AppKernel.php

public function registerBundles()
{
    return array(
        // ...
        new Knp\Bundle\PaginatorBundle\KnpPaginatorBundle(),
        // ...
    );
} 
 
 
 
 
 
The controller :
 
 
        $em    = $this->get('doctrine.orm.entity_manager');
        $dql   = "SELECT a FROM ASFPembedaBundle:Quran a WHERE a.sura = '$surah'";
        $query = $em->createQuery($dql);

        $paginator  = $this->get('knp_paginator');
        $pagination = $paginator->paginate(
           $query,
           $this->get('request')->query->get('page', 1)/*page number*/,
           10/*limit per page*/
        );


        return $this->render('ASFPembedaBundle:Quran:qsurah.html.twig', array(
            'pagination' => $pagination,
            'comments'  => 'ssssss'
        ));
 
 
The view :
 
   <table border=1>
   {# table body #}
   {% for ayat in pagination %}
         <tr {% if loop.index is odd %}class="snippet"{% endif %}>
            <td>{{ ayat.aya }}</td><td>{{ ayat.text }}</td>
         </tr>
         <tr {% if loop.index is odd %}class="grey"{% endif %}>
            <td></td><td>
            <div>
               <p>{{ ayat.quranmakna.mtext }}</p>
            </div>
            </td>
         </tr>
         
    {% else %}
        <p>Search text not found</p>
    {% endfor %}
</table>
   
{# display navigation #}
<div class="navigation">
    {{ knp_pagination_render(pagination) }}
</div> 
 

No comments:

Post a Comment