dphj737575 2014-11-25 19:02
浏览 62
已采纳

使用KnpPaginatorBundle创建和检索可排序选项列表

I'd like to and retrieve sortable options list with KnpPaginatorBundle but I failed. This is the form:

       <form action="" method="get" class="form_sort" id="myForm">
           <span class="manage_title">Sort by:</span>
                <select class="select_styled white_select" id="sort_list" name="option" onChange="sendForm();">
                     <option value="">-------</option>
        {{ knp_pagination_sortable(pagination, 'Country A-Z', 'country', {'sort': 'country', 'direction': 'asc'}) }}
        {{ knp_pagination_sortable(pagination, 'Country Z-Z', 'country', {'sort': 'country', 'direction': 'desc'}) }}
                 </select>
       </form>   

And this is the view in KnpPaginatorBundle\Pagination\sortable_option.html.twig

<option {% for attr, value in options %} {{ attr }}="{{ value }}"{% endfor %}>{{ title }}</option>

and this is the action:

    public function listAction($page, Request $request)
{
    $em = $this->getDoctrine()->getManager();

    $paginator  = $this->get('knp_paginator');

    if ($request->getMethod() == 'POST')
    {

        $option = $request->query->get('option'); //get query option
        list($directiuon, $sort) = explode(":", $option);

        $listTravels = $em->getRepository('ProjectTravelBundle:Travel')->getListTravelsFrontend($sort, $direction);

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

        return $this->render('ProjectFrontendBundle:Frontend:list.html.twig',array(
            'pagination' => $pagination
        ));
    }

    $listTravels = $em->getRepository('ProjectTravelBundle:Travel')->findAll();

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

    return $this->render('ProjectFrontendBundle:Frontend:list.html.twig',array(
        'pagination' => $pagination
    ));
}

I'd like to know how to create the options correctly in the form with sort=country and direction=asc and the second option with sort=country and direction=desc , also I'd like to retrieve those parameters in the controller after POST and correct mistakes in the Action (near explode).

Right now when I select the first choice for example I get this url:

http://localhost/agence/web/app_dev.php/travels?option=Country+A-Z
  • 写回答

1条回答 默认 最新

  • doufu7464 2014-11-25 19:15
    关注

    This happens because you are sending the form, I wrote you a piece of JS in your previous question which:

    • Gets the value of the selected option in eg. localhost/agence/web/app_dev.php/travels?sort=country&direction=desc

    • Redirect to the page you retrieved from the value.

    I think it might also not work because KNPPaginator default template is a link, so instead of href = "KNP_LINK" need to value = "KNP_LINK". Modify the file

    KnpPaginatorBundle \ Pagination \ sortable_option.html.twig
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请分析一下这个电路设计的优点🙏
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 怎么改成输入一个要删除的数后现实剩余的数再输入一个删除的数再现实剩余的数用yes表示继续no结束程序
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取