5
votes

Hi, I have page with a table with a list of elements (index.html.twig.). I'm using KNP Paginator Bundle to paginate the result. Now I want to implement some kind of filters in this page to filter the table result. I'm using AJAX to do this, so I create another view (grupos.html.twig) with the table and the paginator inside to render the result of the query. Here is the controller code:

public function filtrarGrupoPorLetraAction(){
 if ($this->getRequest()->isXmlHttpRequest()) {
   $em = $this->getDoctrine()->getManager();
   $letra = $this->getRequest()->get('letra');
   $entities = $em->getRepository('GrupoBundle:Grupo')->filtrar($letra);

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

   return $this->render('GrupoBundle:Grupo:grupos.html.twig', compact('pagination'));
 }
}

but this code render a new page and I want to pass the result to index.html.twig to render a div.

How can I do this?

2

2 Answers

0
votes

Simply append your result data to your div

-1
votes

if you just need a json response, use the code below

// create a JSON-response with a 200 status code
$response = new Response(json_encode($yourData));
$response->headers->set('Content-Type', 'application/json');

return $response;

if you need to render a a template

return $this->renderView('YourTemplateFile', $yourParams);

hope this helped