Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example needed on custom filtering/sorting #25

Closed
keymaster opened this issue Jun 21, 2011 · 28 comments
Closed

Example needed on custom filtering/sorting #25

keymaster opened this issue Jun 21, 2011 · 28 comments

Comments

@keymaster
Copy link

In the README, there is mention of the ability to add custom filtering and sorting based on the request parameters.

It might be worthwhile adding an example or two of how to use custom filtering and sorting with the PaginatorBundle.

@Dentxinho
Copy link

I'm looking for it. This is a plus over PagerFanta

@l3pp4rd
Copy link
Collaborator

l3pp4rd commented Nov 24, 2011

sorry, hope to cover it for friday

@l3pp4rd
Copy link
Collaborator

l3pp4rd commented Nov 25, 2011

for a start: https://knplabs.com/en/blog/knp-paginator-reborn
later will add example for filtering

@Dentxinho
Copy link

cool!

@franmomu
Copy link
Contributor

franmomu commented Jun 8, 2012

Hi, what about this issue? It would be great to have an example of filtering.

@l3pp4rd
Copy link
Collaborator

l3pp4rd commented Jun 8, 2012

hm, will take a look today ;)

@l3pp4rd
Copy link
Collaborator

l3pp4rd commented Jun 8, 2012

well, there are 2 ways to do it,

  • one to have code in the controller to check the query params and modify the query builder.
  • another one to have a subscriber to modify the query or query builder.

and filter selection which would be controlled by javascript to trigger generated urls.

But in overall, if making those abstract, it would be hard to customize, for example have AND or OR filters. So my advice is to have it in the controller, etc based on query parameters, modify the query. and in template just merge the route parameters for new urls, like pagination template does. This way you would be able to achieve more custom results, which in general I do not see the point to abstract, or make it a part of paginator. Sorting is more predictable.

@franmomu
Copy link
Contributor

franmomu commented Jun 8, 2012

Thank you! I think I'll use query params and modify the query in the controller

@clemblanco
Copy link

What about the status of that filtering feature ?

@elboletaire
Copy link

Still waiting for it!

@gondo
Copy link

gondo commented Dec 9, 2013

any update?
also please see #225

@Bladrak
Copy link

Bladrak commented Jan 3, 2014

👍

@bassrock
Copy link

Any update on this? 👍

@jiguillen
Copy link

Anybody knows how to use filters?

@l3pp4rd
Copy link
Collaborator

l3pp4rd commented Jul 27, 2015

there is no general way since knp paginator supports different kinds of providers for pagination to be built. If you are only using Doctrine ORM in our company we use https://github.com/DATA-DOG/DataDogPagerBundle there you can easily customize filters in pragmatic way.

This was referenced Jul 27, 2015
@lokhman
Copy link

lokhman commented Jan 19, 2016

@l3pp4rd From what I can see DataDogPagerBundle doesn't rely on Doctrine Paginator component, but does simple LIMIT functionality, which won't work with complex one-to-many and many-to-many associations.

@l3pp4rd
Copy link
Collaborator

l3pp4rd commented Jan 22, 2016

@lokhman right, in complex cases you need custom count. we do not make collection joins for that reason, though there is only a single class which may be customized in such cases. for our needs it serves very well.

@deejff
Copy link

deejff commented Nov 3, 2016

It's possioble to implement filters with form component and query builder modifications after submission. You can use this wrapper for KnpPaginator https://github.com/deejff/DataGridBundle to apply this approach.

@Mateuszry
Copy link

I know that this topic outdated, but I was looking for an example implementation of filtering using the KnpPaginatorBundle and I could not find any. Then I finally find sollution by myself. Filtering works only with Query Object. Here is the example:

    $qb = $em->createQueryBuilder()
        ->from('ExampleBundle:Entity', 'entity')
        ->select("entity");
    $page = $request->query->getInt('page', 1);
    $pagination = $this->get('knp_paginator')->paginate(
        $qb,
        $page,
        10
    );

And render filtration in TWIG:

    {{ knp_pagination_filter(pagination, {
        'entity.name': 'Name',
    }) }}

I hope that this sample implementation of filtering will be convenient for someone.

@amiriskander
Copy link

Hello @Mateuszry
Thank you for hinting the twig filtration function knp_pagination_filter.

Do you have any idea how to control the filters created by this function? For example controlling the comparison operator (=, <>, >, <, LIKE) ?

@Mateuszry
Copy link

@amiriskander I'm glad I could help!

I'm not sure if this is the answer to your question, because I haven't used this since the publication of the previous post, but if you want to use operator = you should type something in the filter field, but if you want to use LIKE operator you can type *thing or some* or *thi* and it should work.

@jgrygierek
Copy link

Maybe try this bundle? https://github.com/lexik/LexikFormFilterBundle

@SabWLD
Copy link

SabWLD commented Jun 12, 2019

Bonjour ,
je travaille sur un moteur de recherche selon plusieurs critères avec symfony et php,
pour cela au début je voudrais choisir un filtre et un mot clé. pour pouvoir afficher un liste des projets qui correspond à ma recherche..mais j'ai pas réussi à le faire.
j'ai besoin un aide pour mon code svp

voici mon contrôleur: j'ai une erreur que listprojets not find,

public function research(Request $request, LesProjetsRepository $LesProjetsRepository)
{
    $searchForm = $this->createForm(ResearchType::class);

    $filtre1 = $request->query->getAlnum('caracteristiques');
    $filtre2 = $request->query->getAlnum('filtre2');

    if ( $filtre1 && $filtre2 ) {
       $motcle= $request->get('motcle');
       $this->log->error('test1');

       if (!empty($motcle)) {
           $repo = $this->getDoctrine()->getRepository(LesProjets::class);
           $this->log->error('test5');
           $listprojets = $repo->findProjetsByName($motcle);
        }
    }

    return $this->render('research/index.html.twig', [
        'search_form' => $searchForm->createView(),
        'listProjets' => $listprojets,
    ]);
}

@garak garak closed this as completed Sep 2, 2019
@Eschyle
Copy link

Eschyle commented Dec 17, 2020

I have a problem on my filter.
In my controller I get all my articles that I return in my view.

public function index(Request $request, ArticleRepository $repo, PaginatorInterface $paginator) {

    $searchForm = $this->createForm(SearchType::class);
    $searchForm->handleRequest($request);
    $articles= $repo->findAll();
    $filteredArticles = $paginator->paginate(
        $articles,
        $request->query->getInt('page', 1),
        10,
    );
    return $this->render('default/home.html.twig', [
        'articles' => $filteredArticles
    ]);
}

But in my view when I try to filter then it doesn't seems to work. I wrote that :
{{ knp_pagination_filter( articles, {'article.title': 'title'} ) }}

In the url it returns "http://myUrl/?filterField=article.title&filterValue=japan" but the articles displayed are still the same.

@stalkerxxl
Copy link

I have a problem on my filter. In my controller I get all my articles that I return in my view.

public function index(Request $request, ArticleRepository $repo, PaginatorInterface $paginator) {

    $searchForm = $this->createForm(SearchType::class);
    $searchForm->handleRequest($request);
    $articles= $repo->findAll();
    $filteredArticles = $paginator->paginate(
        $articles,
        $request->query->getInt('page', 1),
        10,
    );
    return $this->render('default/home.html.twig', [
        'articles' => $filteredArticles
    ]);
}

But in my view when I try to filter then it doesn't seems to work. I wrote that : {{ knp_pagination_filter( articles, {'article.title': 'title'} ) }}

In the url it returns "http://myUrl/?filterField=article.title&filterValue=japan" but the articles displayed are still the same.

I have the same problem.. There is a function, but how to use it is not clear.. have you solved this issue?

@garak
Copy link
Collaborator

garak commented Oct 21, 2022

Let me rewrite your example:

public function index(Request $request, ArticleRepository $repo, PaginatorInterface $paginator) {

    $searchForm = $this->createForm(SearchType::class);
    $searchForm->handleRequest($request);
    $articles= $repo->findAll();
    $filteredArticles = $paginator->paginate(
        $articles,  // <-------------- THIS IS JUST WRONG. PLEASE READ THE DOCUMENTATION
        $request->query->getInt('page', 1),
        10,
    );
    return $this->render('default/home.html.twig', [
        'articles' => $filteredArticles
    ]);
}

@stalkerxxl
Copy link

Let me rewrite your example:

public function index(Request $request, ArticleRepository $repo, PaginatorInterface $paginator) {

    $searchForm = $this->createForm(SearchType::class);
    $searchForm->handleRequest($request);
    $articles= $repo->findAll();
    $filteredArticles = $paginator->paginate(
        $articles,  // <-------------- THIS IS JUST WRONG. PLEASE READ THE DOCUMENTATION
        $request->query->getInt('page', 1),
        10,
    );
    return $this->render('default/home.html.twig', [
        'articles' => $filteredArticles
    ]);
}

Strange, but your code works for me (sorting and pagination)...
Yes, I know to pass $qb to the paginator...
My question is rather: why is the second array parameter needed for the
{{ knp_pagination_filter( articles, {'article.title': 'title'} ) }}
method?
What does it affect and how to use it {'article.title': 'title'}?
I did a dump() - it just added additional fields to the form and that's it...
I can't figure out how to use it properly...

@garak
Copy link
Collaborator

garak commented Oct 21, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests