PUGX / filter-bundle

Utility for filtering/sorting with Symfony
GNU Lesser General Public License v3.0
7 stars 0 forks source link

Code example for sort #3

Closed vittore closed 2 years ago

vittore commented 2 years ago

Hi,

is there a simple full code example (controller and template) about sorting? I'm trying to mix:

The target is a simple, completed, and nice boostrap5 CRUD interface.

Thanks in advance. Vittore.

garak commented 2 years ago

Sure! Here's an example of controller, with list and sort actions:

<?php

class MyController
{
    #[Route('list', name: 'foo_list', methods: [Request::METHOD_GET])]
    public function lista(Repository $repository, Filter $filter, Request $request): Response
    {   
        if ($filter->saveFilter(FooFilterType::class, 'foo')) {
            return $this->redirectToRoute('foo_list');
        }
        $pagination = $repository->paginate($filter->filter('foo'), $request->query->getInt('page', 1));

        return $this->render('foo/list.html.twig', [
            'form' => $filter->getFormView('foo'),
            'pagination' => $pagination,
        ]);
    }

    #[Route('/sort/{field}/{direction}', name: 'foo_sort', requirements: ['direction' => 'ASC|DESC'], methods: [Request::METHOD_GET])]
    public function sort(Filter $filter, string $field, string $direction = 'ASC'): Response
    {
        $filter->sort('foo', $field, $direction);

        return $this->redirectToRoute('foo_list');
    }
}

The "list" should be already covered in examples ayway. The same examples are showing what to do in templates.

Let me know if you need more info

vittore commented 2 years ago

Perfect. Thanks a lot.

v.

PS: is there any documentation about multi sort?

garak commented 2 years ago

PS: is there any documentation about multi sort?

Sorry, multi-sorting is not supported