管理申报表和结果页面的最佳方式 [英] Best way to manage filer form and result page

查看:24
本文介绍了管理申报表和结果页面的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Symfony 中管理过滤器页面和结果页面的最佳方式是什么?我有一个管理过滤器表单和执行查询的控制器.此查询的结果必须传入另一个控制器操作.结果必须显示在另一个控制器操作中,因为我使用了 knp_paginator.如果我在过滤器表单的相同控制器操作中呈现结果,则在更改页面时控制器显示过滤器表单而不是结果.

What's the best way to manage filter page and result page in Symfony? I have a controller that manage filter form and execute query. The result of this query must pass in another controller action. The result must show in another controller action because I used knp_paginator. If I render the result in same controller action of filter form, when change page the controller show filter form and not result.

推荐答案

这是我使用的方法:

创建查找表单的操作:

public function findAction(Request $request)
{

    $form = $this->createFindForm($request);

    $form->handleRequest($request);

    if(($form->isSubmitted() && !$request->isXmlHttpRequest()))
    {
        if(($this->isValidFindForm($form) && $form->isValid()))
        {
            $parm = $request->request->get('findForm');
            return $this->redirect($this->generateUrl('list_documents',$parm));
        }
    }

    return $this->render(
            'myBundle:Documents:document\document_find.html.twig',
            array('form' => $form->createView())
    );
}

private function createFindForm(Request $request)
{
    $form = $this->createForm(
                new findDocumentType(
                    $this->getDoctrine()->getManager(),
                    $request
                ),
                null,
                array(
                    'action' => $this->generateUrl('find_documents'),
                    'method' => 'POST',
                )
            );
    return $form;
}

我使用了 $parm = $request->request->get('findForm');获取查询字符串.findForm"是我的过滤器表单的名称.

I used $parm = $request->request->get('findForm'); to get the querystring. "findForm" is the name of my filter form.

重定向操作:

public function listAction(Request $request)
{
    $documents = $this->searchDocument($request);
    $paginator = $this->get('knp_paginator');
    $pagination = $paginator->paginate(
            $documents,
            $this->get('request')->query->get('page', 1)
    );
    return $this->render(
            'myBundle:Documents:document\document_list.html.twig',
            array('pagination' => $pagination)
    );
}

请求传递给搜索函数(请求包含查询字符串)在搜索操作中:

The request is passed to search function (request contains querystring) In search action:

private function searchDocument(Request $request)
{
    $parm = $request->query;
    $repository = $this->getDoctrine()->getRepository('docliteBundle:Document\\document');
    $query = $repository
                ->createQueryBuilder('d');
 ....

使用 $request->query->get() 我可以访问所有参数.

With $request->query->get() I have access to all parameter.

希望这对某人有所帮助.

Hope this help someone.

附言对于我使用的分页 KNP_paginator

P.S. for the pagination I used KNP_paginator

这篇关于管理申报表和结果页面的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆