Commit 0af45a95 authored by Mat's avatar Mat
Browse files

potage_legume_read: pagination

parent cd0d059b
......@@ -29,7 +29,7 @@ class LegumeController extends Controller
$em->persist($legume);
$em->flush();
return $this->redirectToRoute('potage_legume_read');
return $this->redirectToRoute('potage_legume_read', array('page'=> 1));
}
return $this->render('@Potage/Legume/create.html.twig', array(
......@@ -42,11 +42,25 @@ class LegumeController extends Controller
* @return \Symfony\Component\HttpFoundation\Response
*
*/
public function readAction()
public function readAction($page)
{
$legumes = $this->getDoctrine()->getManager()->getRepository('PotageBundle:Legume')->findAllByRead();
if ($page < 1) {
throw $this->createNotFoundException("Page ".$page." inexistante");
}
$nbPerPage = 10;
$em = $this->getDoctrine()->getManager();
$legumes = $em->getRepository('PotageBundle:Legume')->findAllByRead($page, $nbPerPage);
$nbPages = ceil(count($legumes)/$nbPerPage);
if ($page > $nbPerPage) {
throw $this->createNotFoundException("Page ".$page." inexistante");
}
return $this->render('@Potage/Legume/read.html.twig', array(
'legumes' => $legumes
'legumes' => $legumes,
'nbPages' => $nbPages,
'page' => $page
));
}
......@@ -72,7 +86,7 @@ class LegumeController extends Controller
$em->persist($legume);
$em->flush();
return $this->redirectToRoute('potage_legume_read');
return $this->redirectToRoute('potage_legume_read', array('page'=> 1));
}
return $this->render('@Potage/Legume/update.html.twig', array(
......@@ -95,7 +109,7 @@ class LegumeController extends Controller
$em->remove($legume);
$em->flush();
return $this->redirectToRoute('potage_legume_read');
return $this->redirectToRoute('potage_legume_read', array('page'=> 1));
}
}
......
......@@ -2,6 +2,9 @@
namespace PotageBundle\Repository;
use Doctrine\ORM\Tools\Pagination\Paginator;
/**
* LegumeRepository
*
......@@ -15,12 +18,16 @@ class LegumeRepository extends \Doctrine\ORM\EntityRepository
*
* @return Legume[]
*/
public function findAllByRead()
public function findAllByRead($page, $nbPerPage)
{
$qb = $this->createQueryBuilder('l')
->orderBy('l.nom' , 'ASC');
->orderBy('l.id' , 'ASC')
->getQuery();
$qb
->setFirstResult(($page-1)* $nbPerPage)
->setMaxResults($nbPerPage);
return $qb->getQuery()->getResult();
return new Paginator($qb, true); ;
}
......
......@@ -11,7 +11,7 @@ potage_legume_create:
_controller: PotageBundle:Legume:create
potage_legume_read:
path: /legumes
path: /legumes/{page}
defaults:
_controller: PotageBundle:Legume:read
......
File mode changed from 100644 to 100755
......@@ -15,7 +15,7 @@
<span class="sr-only">(current)</span></a>
</li>
<li class="nav-item {{ app.request.get('_route') == 'potage_legume_read' ? 'active' }}">
<a class="nav-link" href="{{ path('potage_legume_read') }}">Les légumes
<a class="nav-link" href="{{ path('potage_legume_read', {'page': 1}) }}">Les légumes
<span class="sr-only">(current)</span></a>
</li>
{#
......
......@@ -11,7 +11,7 @@
<h1>{{ block('title') }}</h1>
<p>
<a href="{{ path('potage_legume_read') }}"><i class="fas fa-arrow-left fa-fw"></i>Retour vers la liste</a>
<a href="{{ path('potage_legume_read', {'page': 1}) }}"><i class="fas fa-arrow-left fa-fw"></i>Retour vers la liste</a>
</p>
<div class="row">
......
File mode changed from 100644 to 100755
......@@ -47,4 +47,26 @@
{% endfor %}
</tbody>
</table>
<nav aria-label="Page navigation">
<ul class="pagination justify-content-center">
<li class="page-item disabled">
<a class="page-link" href="#" tabindex="-1" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
<span class="sr-only">Précédent</span>
</a>
</li>
{% for p in range(1, nbPages)%}
<li class="page-item {% if p == page %}active{% endif %}">
<a class="page-link" href="{{ path('potage_legume_read', {'page': p}) }}">{{ p }}</a>
</li>
{% endfor %}
<li class="page-item">
<a class="page-link" href="#" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
<span class="sr-only">Suivant</span>
</a>
</li>
</ul>
</nav>
{% endblock %}
......@@ -11,7 +11,7 @@
<h1>{{ block('title') }}</h1>
<p>
<a href="{{ path('potage_legume_read') }}"><i class="fas fa-arrow-left fa-fw"></i>Retour vers la liste</a>
<a href="{{ path('potage_legume_read', {'page': 1}) }}"><i class="fas fa-arrow-left fa-fw"></i>Retour vers la liste</a>
</p>
......
File mode changed from 100644 to 100755
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment