Symfony 4-分页过滤器搜索问题

时间:2019-09-14 09:52:52

标签: php symfony pagination

在我的项目Symfony 4中,我创建了一个分页服务。 在我的控制器中,我这样声明:

$pagination->setEntityClass(User::class)
                ->setLimit(5)
                ->setPage($page);

在这种情况下,它会显示我所有带有分页的用户的列表。

但是在我的页面中,我插入了一个搜索框,以按用户的名字或姓氏搜索用户。

Image

当用户执行搜索并通过单击“搜索”提交表单时,在我的控制器中,我会进行一次检查。

$name = $request->get('champRecherche');

如果$名称为null,则执行全局搜索,如上面的代码所示。 但是,如果$ name与null不同,那么我将在搜索字段中提交带有某些内容的搜索表单,在这种情况下,代码将是:

$parametres = ['name' => $name];
            $pagination->setEntityClass(User::class)
                ->setLimit(2)
                ->setPage($page)
                ->setFunctionToCall('getUsersByName')
                ->setParameters($parametres);

我告诉服务库应该使用什么功能,然后插入参数。

所以一切正常。如果我在搜索栏中输入了一些内容并进行了验证,那么按下面的分页可以很好地满足我的要求。

但是突然间,如果有几页,并且我要求转到第2页,那么在控制器中,他将认为我要求进行全局搜索,因为没有提交搜索表单,因此向我显示页码我已过滤的用户列表中的2,它将向我显示所有用户的页面2。那就是我不知道如何处理这个问题的地方

我的树枝:

{% extends 'base.html.twig' %}


{% block body %}


    <form action="{{ path('rh_index') }}" method="POST">

        <input class="form-control" type="hidden" name="token" value="{{ csrf_token('rh_index') }}">
        <div class="row">
            <div class="col-6">
                <input class="form-control" placeholder="Entrer le nom de l'utilisateur" type="text" name="champRecherche" id="champRecherche"/>
            </div>
            <div class="col">
                <button type="submit" class="btn btn-primary" id="search" value="Chercher">Chercher</button>
                <a href="{{ path('rh_index') }}" class="btn btn-primary">Afficher tous</a>
            </div>
        </div>


    </form><br>
    <table class="table table-hover align-items-center">
        <thead>
            <tr>
                <th>Nom</th>
                <th>Service</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            {% for user in pagination.data %}
                <tr>
                    <th>{{user.fullName}}</th>
                    <th>{{user.groupe.nom}}</th>
                    <th>
                        <a href="#" class="btn btn-primary">Voir</a>
                    </th>
                </tr>
            {% endfor %}
        </tbody>
    </table>
    <br>


    {{pagination.display}}



{% endblock %}

我的控制器:

/**
     * @Route("/rh/{page<\d+>?1}", name="rh_index")
     */
    public function index($page, Request $request, PaginationService $pagination)
    {


        $name = $request->get('champRecherche');


        if ($name != null) {
            $token = $request->get('token');
            if (!$this->isCsrfTokenValid('rh_index', $token)) {
                throw new \Symfony\Component\Security\Core\Exception\AccessDeniedException('Accès interdit');
            }

            //$pagination = $repoUser->getUsersByName($name);
            $parametres = ['name' => $name];
            $pagination->setEntityClass(User::class)
                ->setLimit(1)
                ->setPage($page)
                ->setFunctionToCall('getUsersByName')
                ->setParameters($parametres);
        } else {
            $pagination->setEntityClass(User::class)
                ->setLimit(5)
                ->setPage($page);

            // $users = $repoUser->getUsers();
        }

        return $this->render('rh/index.html.twig', [
            'pagination' => $pagination
        ]);
    }

我的服务显示功能:

 /**
     * Permet d'afficher le rendu de la navigation au sein d'un template twig !
     * 
     * On se sert ici de notre moteur de rendu afin de compiler le template qui se trouve au chemin
     * de notre propriété $templatePath, en lui passant les variables :
     * - page  => La page actuelle sur laquelle on se trouve
     * - pages => le nombre total de pages qui existent
     * - route => le nom de la route à utiliser pour les liens de navigation
     *
     * Attention : cette fonction ne retourne rien, elle affiche directement le rendu
     * 
     * @return void
     */
    public function display()
    {
        //  $parametres = $this->convertParametersForTwig();
        // dd($parametres);

        $this->twig->display($this->templatePath, [
            'page' => $this->currentPage,
            'pages' => $this->getPages(),
            'route' => $this->route,
        ]);
    }

还有我的分页模板:

<div class="d-flex justify-content-center">
    <ul class="pagination">
        <li class="page-item {% if page == 1 %}disabled{% endif %}">
            <a class="page-link" href="{{path(route,{'page': page - 1})}}">&laquo;</a>
        </li>
        {% for i in 1..pages %}
            <li class="page-item {% if page == i %}active {% endif %}">
                <a class="page-link" href="{{path(route,{'page':i})}}">{{i}}</a>
            </li>
        {% endfor %}

        <li class="page-item {% if page == pages %}disabled{% endif %}">
            <a class="page-link" href="{{path(route,{'page': page + 1})}}">&raquo;</a>
        </li>
    </ul>
</div>

0 个答案:

没有答案
相关问题