搜索结果与传呼机房塔相结合

问题描述 投票:0回答:1

我有一个应用程序,它显示一个项目列表,在主页面我有两个输入的添加表单,第一个包含月份值,第二个包含年份值;我在控制器内设置了一个动作,在该动作中有一个函数,接受两个参数 "月份,年份"。

/**
 * @Route("/bills",defaults={"page": "1"} , name="bills", methods={"GET"})
 * @Route("/bills/page/{page<[1-9]\d*>}", name="bills_paginated", methods="GET")
 * @param BillRepository $billRepository
 * @param Request $request
 * @param int $page
 * @return Response
 */
public function indexBills(BillRepository $billRepository,Request $request, int $page): Response
{
    $formBills = $this->createForm(BillsType::class);
    $formBills->handleRequest($request);
    $getArgs= $request->query->get('bills');
    $Bills =$billRepository->unpaidBills($getArgs,$page);

    return $this->render('homePage/bills.html.twig', [
        'formBills'=>$formBills->createView(),
        'Bills'=> $Bills ,
    ]);
}

当我提交搜索表单时,搜索结果按照预期显示,页面分页也在那里,但当我点击下一页时,例如'2'页,发生这种情况的原因在于 $getArgs 是空的,是否有办法在改变页面时保持相同的参数。

谢谢你:)

php symfony symfony4 symfony-4.3 pagerfanta
1个回答
0
投票

当你点击 "第2页 "时,你没有发送 "年 "和 "月 "的数据。

解决方案1 - 注入 bills 从控制人那里得到的数据

我想,最简单的方法是把 $request->query->get('bills') 挨着树枝 formData 例如,在你的小树枝上添加以下内容 'bills' : formData 途径参数中的 bills_paginated

如果你这样做,symfony会将你的表单数据添加到你的url中,当你改变你的页面时,你不会丢失它们,所以当你进行 $getArgs= $request->query->get('bills'); 你有你的数据

解决方案2--获得 bills 形成数据

这是最简单的解决方法,直接在twig中获取账单表格数据,像这样。{% set formData = app.request.get('bills') %}.然后在你的 twig 中添加 'bills' : formData 途径参数中的 bills_paginated

最佳解决方案

IMO最好的和最简洁的解决方案是像这样的年和月的路线。"/bills/year/{year<[1-9]\d*>}/month/{month<[1-9]\d*>}/page/{page<[1-9]\d*>}"


0
投票

I have figure it out myself , Thank you everyone anyone.so if someone faces same problem as mine here is how I fixed it:

在twig的搜索结果显示的页面内,在底部的pagerfanta分页功能内,我添加了一 routeParams.

{%
  set params = app.request.get('bills')
%}

{% if Bills.haveToPaginate %}
      <div class="card-footer pagination">
        {{
           pagerfanta(Bills, 'twitter_bootstrap4_translated',
             {
              routeName: 'bills_paginated',
              routeParams:{bills:params}
             })
        }}
      </div>


{% endif %}

现在每当我点击一个特定的页面 params 我希望代码足够清晰,并对你有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.