0
votes

I'm new to Symfony, I got a page in where I have a list of articles, and I created a form above it that allow me to filter the results of my query : enter image description here

The form works fine, but when I click on the 2nd page of my paginator table, the form reset, so my filter is reset too. What I want is that we can filter results and be able to paginate the table, with the same filter, and that the form keep his data.

Here's my code right now :

Controller :

public function listeAction(Request $request, $page) {
        if ($page < 1 && strlen($page) != 0) {
            throw new NotFoundHttpException('Page "' . $page . '" inexistante.');
        } else if (strlen($page) == 0) {
            $page = 1;
        }

        $nbPerPage = 5;

        $form = $this->createForm(ArticleRechercheType::class);
        $form->setData(array('rds' => true));

        //if we use filters
        if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
            $data = $form->getData();
            $form = $this->createForm(ArticleRechercheType::class, $data);
            if ($data['famille'] != null) {
                $data['famille'] = $data['famille']->getId();
            }
            $listArticles = $this->getDoctrine()
                    ->getManager()
                    ->getRepository('GRBackOfficeBundle:Article')
                    ->getFilteredArticles($page, $nbPerPage, $data);
        } else {
            $data = $form->getData();
            $form = $this->createForm(ArticleRechercheType::class, $data);

            $listArticles = $this->getDoctrine()
                    ->getManager()
                    ->getRepository('GRBackOfficeBundle:Article')
                    ->getArticles($page, $nbPerPage);
        }

        $nbPages = ceil(count($listArticles) / $nbPerPage);

        if ($nbPages != 0 && $page > $nbPages) {
            throw new NotFoundHttpException('Page "' . $page . '" inexistante.');
        }

        if ($nbPages == 0) {
            $nbPages = 1;
        }

        return $this->render('GRBackOfficeBundle:Article:liste_articles.html.twig', array(
                    'form' => $form->createView(),
                    'listArticles' => $listArticles,
                    'nbPages' => $nbPages,
                    'page' => $page
        ));
    }

Form :

class ArticleRechercheType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder->add('famille', EntityType::class, array(
                    'class' => 'GRBackOfficeBundle:Famille',
                    'choice_label' => 'nom',
                    'required' => false
                ))
                ->add('rds', CheckboxType::class, array('required' => false))
                ->add('recherche', TextType::class, array('required' => false))
                ->add('Rechercher', SubmitType::class);
    }

    public function configureOptions(OptionsResolver $resolver) {
        $resolver->setDefaults(array(
            'translation_domain' => false
        ));
    }

}

View :

{% extends "GRBackOfficeBundle::layout.html.twig" %}

{% block title %}
    Liste des articles
{% endblock %}

{% block gr_bo_body %}
    <h2>Liste des articles</h2>

    <div class="row">
        <div class="well row">
            {{ form_start(form) }}

            {{ form_errors(form) }}

            <div class="form-group col-md-2">
                {{ form_label(form.famille, "Famille d'article", {'label_attr': {'class': 'control-label'}}) }}

                {{ form_errors(form.famille) }}

                {{ form_widget(form.famille, {'attr': {'class': 'form-control'}}) }}
            </div>

            <div class="form-group col-md-4">
                {{ form_widget(form.rds, {'attr': {'class': ''}}) }}
                {{ form_label(form.rds, "Montrer les articles en rupture de stock", {'label_attr': {'class': 'control-label'}}) }}
                {{ form_errors(form.rds) }}


            </div>

            <div class="form-group col-md-3">
                {{ form_label(form.recherche, "Recherche", {'label_attr': {'class': 'control-label'}}) }}

                {{ form_errors(form.recherche) }}

                {{ form_widget(form.recherche, {'attr': {'class': 'form-control'}}) }}
            </div>
            <div class="form-group col-md-3" style="text-align: center">
                {{ form_widget(form.Rechercher, {'attr': {'class': 'btn btn-primary'}}) }}
            </div>
            {{ form_rest(form) }}

            {{ form_end(form) }}
        </div>

        <div class="well row">
            <table class="table table-bordered table-striped" id="listeArticles" style="width: 100%" cellspacing="0">
                <thead>
                    <tr>
                        <th>Référence client</th>
                        <th>Référence interne</th>
                        <th>Famille</th>
                        <th>Libellé</th>
                        <th>Alerte</th>
                        <th>Stock</th>
                    </tr>
                </thead>
                <tbody id="bodyListeArticles">
                    {% for article in listArticles %}
                        <tr>
                            <td><a href="{{ path('gr_bo_modif_article', {'article_id': article.id}) }}">{{ article.RefArticle }}</a></td>
                            <td>{{ article.RefLogistique }}</td>
                            <td>{{ article.famille.nom }}</td>
                            <td>{{ article.libelle }}</td>
                            <td>{{ article.StockAlerte }}</td>
                            <td>{{ article.StockActuel }}</td>
                        </tr>
                    {% endfor %}
                </tbody>
            </table>
            {% if nbPages > 1 %}
                <ul class="pagination">
                    {% for p in range(1, nbPages) %}
                        <li {% if p == page %} class="active"{% endif %}>
                            <a href="{{path('gr_bo_liste_articles', {'page' : p}) }}">{{ p }}</a>
                        </li>
                    {% endfor %}
                </ul>
            {% endif %}
        </div>

    </div>

{% endblock %}

If anyone have an idea of how I can do that, that will be appreciated

1

1 Answers

1
votes

You can use a bundle (like the knp paginator bundle for exemple), or put your args in session. But the bundle will make it simple as it does everything for you, you just have to pass your datas