2012-12-20 83 views
5

嗨,我有一個表與元素列表(index.html.twig。)的表。我使用KNP Paginator Bundle對結果進行分頁。現在我想在此頁面中實現某種過濾器來過濾表結果。我使用AJAX來做到這一點,所以我創建了另一個視圖(grupos.html.twig),其中的表格和paginator內部呈現查詢結果。 這裏是控制器代碼:Symfony 2.1 ajax過濾器與KNP分頁程序包

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

但是這個代碼呈現了新的一頁,我想結果傳遞給index.html.twig呈現一個div。

我該怎麼做?

回答

-1

如果你只需要一個JSON響應,請使用以下

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

return $response; 

的代碼,如果你需要渲染AA模板

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

希望這有助於

0

只需添加你的結果數據您的div

+0

也許添加一個代碼示例? – i3arnon