EasyCorp / EasyAdminBundle

EasyAdmin is a fast, beautiful and modern admin generator for Symfony applications.
MIT License
4.04k stars 1.02k forks source link

Default Filter list #3665

Open seb-jean opened 4 years ago

seb-jean commented 4 years ago

Hi,

Is there a function to filter the list by default?

We already have "setDefaultSort" to sort it. As a reminder, here is the code below:

public function configureCrud(Crud $crud): Crud
{
    return $crud
        ->setDefaultSort([
            'name' => 'ASC',
        ]);
}

It would be great to have setDefaultFilter().

Thanks !

ceochronos commented 4 years ago

Hi, sound interesting. What use case do you want to solve?

seb-jean commented 4 years ago

For example:

public function configureCrud(Crud $crud): Crud
{
    return $crud
        [...]
        ->addDefaultFilter('categories', ComparisonType::EQ, $category);
}
jpRsah commented 4 years ago

For example:

public function configureCrud(Crud $crud): Crud
{
    return $crud
        [...]
        ->addDefaultFilter('categories', ComparisonType::EQ, $category);
}

I have not this method in class Crud. Which version have this method? my version is 3.1

seb-jean commented 4 years ago

No, that's an example.

ceochronos commented 4 years ago

For example:

public function configureCrud(Crud $crud): Crud
{
    return $crud
        [...]
        ->addDefaultFilter('categories', ComparisonType::EQ, $category);
}

Nice, How do you get the $category variable?

I may have a similar use case, see https://github.com/EasyCorp/EasyAdminBundle/issues/3663#issue-675627632

// in webinarCrudController

    public function ViewParticipants(AdminContext $context)
    {
        $webinar = $context->getEntity()->getInstance();

        $url = $this->crudUrlGenerator->build()
            ->unset('entityId')
            ->setController(ParticipantCrudController::class)
            ->setAction(Action::INDEX)
            ->set('filters[id_webinar][comparison]', '=')
            ->set('filters[id_webinar][value]', $webinar->getId())
            ->generateUrl();

        return $this->redirect($url);
    }

So, from webinar index you can go to participant index having it filtered with the participant enrolled in the said webinar

seb-jean commented 4 years ago

$category will have to be generated. For example:

$category = $this->repository->findOneBy(['name' => 'Category 1']);
seb-jean commented 4 years ago

For example:

public function configureCrud(Crud $crud): Crud
{
    return $crud
        [...]
        ->addDefaultFilter('categories', ComparisonType::EQ, $category);
}

Nice, How do you get the $category variable?

I may have a similar use case, see #3663 (comment)

// in webinarCrudController

    public function ViewParticipants(AdminContext $context)
    {
        $webinar = $context->getEntity()->getInstance();

        $url = $this->crudUrlGenerator->build()
            ->unset('entityId')
            ->setController(ParticipantCrudController::class)
            ->setAction(Action::INDEX)
            ->set('filters[id_webinar][comparison]', '=')
            ->set('filters[id_webinar][value]', $webinar->getId())
            ->generateUrl();

        return $this->redirect($url);
    }

So, from webinar index you can go to participant index having it filtered with the participant enrolled in the said webinar

Your way gets around the problem, but I don't think it's the best way.

You should just indicate the name of the property, the type of comparison and the value(s) to be compared.

rogergerecke commented 4 years ago

look in my source best use createIndexQueryBuilder overwrite method as example https://github.com/rogergerecke/alt-sym/blob/master/src/Controller/User/HostelCrudController.php#L273-L285

https://github.com/rogergerecke/alt-sym/blob/master/src/Controller/User/HostelCrudController.php#L309-L323

emi87290 commented 4 years ago

look in my source best use createIndexQueryBuilder overwrite method as example

Your solution is nice if we want to filter permanently but how to get a default filter that will be of when adding filters? I mean I want to display only current year records, but when I filter to get past year, I want to see them. If I do the filter in createIndexQueryBuilder, I wont see past year records.

rogergerecke commented 4 years ago

https://symfony.com/doc/master/bundles/EasyAdminBundle/filters.html

emi87290 commented 4 years ago

https://symfony.com/doc/master/bundles/EasyAdminBundle/filters.html

Your link is good if we want to use filter but I want to filter by default my result and cancel this filter when a dynamic filter is used. I went this way: if(!isset($_GET['filters'])) { $response->andWhere('YEAR(entity.date_creation) = :annee')->setParameter('annee', date('Y')); }

Edit: fixed style

rogergerecke commented 4 years ago

createIndexQueryBuilder ther you can handle with the request object its Symfony based on PHP

tsikora666 commented 2 years ago

createIndexQueryBuilder ther you can handle with the request object its Symfony based on PHP

Yes, but filter widget won't be initialized This way :(

rogercbe commented 1 year ago

Had the same situation and solved it with a different approach. Since filters are defined as query params, you can add those on the MenuItem Link. Filters widget will work as expected and the filter will be enabled as default.

MenuItem::linkToCrud('Users', 'fa fa-user', User::class)->setQueryParameter('filters[deletedAt]', "null")

Keep in mind that since it'll be parsed as string, null has to be stringified as "null" otherwise it will not work because it will not be appended to the url.