简体   繁体   English

带有搜索表格的Knp分页器

[英]Knp Paginator with search form

I'm trying to use KNP paginator with a form-search. 我正在尝试使用KNP分页器进行表单搜索。

Everything works fine, but when I go to page 2 they send me back to page 2 without the result of the search. 一切正常,但是当我转到第2页时,他们将我带回到第2页,而没有搜索结果。

I used POST methods on my twig because if I use GET methods, nothing happening. 我在树枝上使用了POST方法,因为如果使用GET方法,则什么都不会发生。

I found many post on my issue but I don't know how can I resolve it. 我发现了很多关于该问题的帖子,但我不知道该如何解决。 If someone has some advices for me, please can you help me please. 如果有人对我有一些建议,请您能帮我吗。

This is my code : 这是我的代码:

 public function rechercheAction() { $form = $this->createForm(RechercheClientType::class); return $this->render('admin/client/recherche.html.twig',[ 'form' => $form->createView() ] ); } /** * @Route("/admin/client/recherche", name="recherche_client") */ public function rechercheTraitementAction(PaginatorInterface $paginator,Request $request) { $session = $request->getSession(); $form = $this->createForm(RechercheClientType::class); $form->handleRequest($request); $query = $this->repository->recherche($form['rechercheClient']->getData()); $users = $paginator->paginate( $query, $request->query->getInt('page',1), 10 ); //Compte le nombre d'éléments recherchés $count = count($query); return $this->render('admin/client/data-tables.html.twig',[ 'users' => $users, 'count' =>$count, ] ); } 

 <!-- recherche.html.twig --> <form action="{{ path('recherche_client') }}" method="post"> {{ form_widget(form.rechercheClient, { 'attr': { 'class' : "form-control",'placeholder': "Rechercher par ID, nom , prénom, mail ou société"} }) }} {{ form_widget(form) }} </form> <!-- data-table.html.twig --> <div class="" style="float:right"> {{ knp_pagination_render(users) }} </div> 

 public function recherche($chaine) { return $this->createQueryBuilder('u') ->andWhere('u.nom like :chaine') ->orWhere('u.prenom like :chaine') ->orWhere('u.email like :chaine') ->orWhere('u.societe like :chaine') ->orderBy('u.id') ->setParameter('chaine','%'.$chaine.'%') ->getQuery() ->getResult(); } 

Amigo I had the same issue but since we have interpreted the code differently I am gonna show my example. 我遇到了同样的问题,但是由于我们对代码的解释不同,因此我将展示我的示例。

$searchForm = $this->createFormBuilder($property)
            ->add('type', EntityType::class, [
                'class' => Type::class,
                'choice_label' => 'name',
                'mapped' => false,
                'expanded' => true,
                'multiple' => true,
                'label' => false,
            ])
            ->add('filter', SubmitType::class, [
                'attr' => [
                    'class' => 'btn btn-outline-dark btn-rounded waves-effect'
                ]
            ])
            ->setMethod('GET')
            ->getForm();

the ->setMethod('GET') made the difference ->setMethod('GET')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM