EasyAdminBundle icon indicating copy to clipboard operation
EasyAdminBundle copied to clipboard

Enable filter by default

Open indapublic opened this issue 4 years ago • 13 comments

How to enable filter by default in Crud Controller?

public function configureFilters(Filters $filters): Filters
    {
        return $filters
            ->add("resolved")
        ;
    }

I can use createIndexQueryBuilder but this condition is impossible to remove in UI

indapublic avatar Nov 07 '20 04:11 indapublic

Probably setDefaultFilter like setDefaultSort you have already

indapublic avatar Nov 07 '20 04:11 indapublic

`<?php namespace App\Controller\Admin;

use Doctrine\ORM\QueryBuilder; use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection; use EasyCorp\Bundle\EasyAdminBundle\Collection\FilterCollection; use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController; use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto; use EasyCorp\Bundle\EasyAdminBundle\Dto\SearchDto; use EasyCorp\Bundle\EasyAdminBundle\Orm\EntityRepository;

abstract class CustomCrudController extends AbstractCrudController { public function createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityDto, FieldCollection $fields, FilterCollection $filters): QueryBuilder { $qb = $this->get(EntityRepository::class)->createQueryBuilder($searchDto, $entityDto, $fields, $filters); $qb->andWhere('entity.user = :user'); $qb->setParameter('user', $this->getUser()); return $qb; } }`

Mepcuk avatar Nov 11 '20 07:11 Mepcuk

You are welcome :)

Mepcuk avatar Nov 11 '20 07:11 Mepcuk

I wrote about it, it's not solution

indapublic avatar Nov 11 '20 07:11 indapublic

No, this we already use extensively. Problem is you cannot unset the filter that way

parijke avatar Nov 11 '20 08:11 parijke

You always can modify the admin link to add the filter on it.

So when you click on the link from the menu, the filter is applied directly, no ?

You can create another Crud class that extend your not filtered CRUD class and modify the queryBuilder to have a filtered listing and link in a submenu of the parent CRUD for exemple.

You have many possibilities.

ioniks avatar Nov 14 '20 21:11 ioniks

I do ik like this in the DashboardController:

 yield MenuItem::linkToCrud('Policies', 'fa fa-paperclip', InsurancePolicy::class)
            ->setQueryParameter('filters[status][comparison]', '=')
            ->setQueryParameter('filters[status][value]', 'active')
        ;

parijke avatar Nov 26 '20 13:11 parijke

Thanks @parijke I'll check it.

indapublic avatar Nov 26 '20 22:11 indapublic

@parijke thank you, this solution is an acceptable workaround :)

But still, I'd suggest a new setDefaultFilters() property which would be used inside the configureCrud() method. It could look something like this:

->setDefaultFilters([
    'date' => [
        'comparison' => BooleanFilter::IS_EQUAL_TO,
        'value' => new DateTime('today')
    ],
])

tuc0w avatar Jan 07 '22 13:01 tuc0w

@parijke thank you, this solution work very nice in my case. My suggestion will be to create new mehtod to set filter

My class looks like this now

public function configureFilters(Filters $filters): Filters
    {
        return $filters
        ->add(BooleanFilter::new('isDone'))
        ->add(DateCalendarFilter::new('Date'));
    }

and after It can look like this, of course name of method can be better

public function configureFilters(Filters $filters): Filters
    {
        return $filters
        ->add(BooleanFilter::new('isDone')->default('isDone' => true))
        ->add(DateCalendarFilter::new('Date')->default('Date' => 'today'));
    }

pawelwaw-git avatar Oct 28 '22 15:10 pawelwaw-git

I do ik like this in the DashboardController:

 yield MenuItem::linkToCrud('Policies', 'fa fa-paperclip', InsurancePolicy::class)
     ->setQueryParameter('filters[status][comparison]', '=')
     ->setQueryParameter('filters[status][value]', 'active')
;

Thank you! I'm using this as a fallback.

If anyone (like me) has to check whether a value is null or not, they need to:

 yield MenuItem::linkToCrud('Policies', 'fa fa-paperclip', InsurancePolicy::class)
     ->setQueryParameter('filters[status][value]', 'null')
     // ->setQueryParameter('filters[status][value]', 'not_null')
;

Just wanted to leave this here in case anyone needs it.

AmProsius avatar May 14 '23 18:05 AmProsius

I do ik like this in the DashboardController:

 yield MenuItem::linkToCrud('Policies', 'fa fa-paperclip', InsurancePolicy::class)
            ->setQueryParameter('filters[status][comparison]', '=')
            ->setQueryParameter('filters[status][value]', 'active')
        ;

Hi, i adapted this to filtering date using "is between", but which file to look for knowning all the comparison operation value?

        MenuItem::linkToCrud('Policies', 'fa fa-paperclip', InsurancePolicy::class)
            ->setQueryParameter('filters[date][comparison]', 'between')
            ->setQueryParameter('filters[date][value]', '2024-01-01')
            ->setQueryParameter('filters[date][value2]', '2024-02-01'),

nurradityam avatar Mar 18 '24 03:03 nurradityam

For a BooleanFilter use

yield MenuItem::linkToCrud('Clients', 'fa fa-building', Client::class)
            ->setQueryParameter('filters[status]', 1);

alexandru-burca avatar Jun 20 '24 20:06 alexandru-burca