Open seb-jean opened 4 years ago
Hi, sound interesting. What use case do you want to solve?
For example:
public function configureCrud(Crud $crud): Crud
{
return $crud
[...]
->addDefaultFilter('categories', ComparisonType::EQ, $category);
}
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
No, that's an example.
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
$category
will have to be generated.
For example:
$category = $this->repository->findOneBy(['name' => 'Category 1']);
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.
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
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.
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
createIndexQueryBuilder ther you can handle with the request object its Symfony based on PHP
createIndexQueryBuilder ther you can handle with the request object its Symfony based on PHP
Yes, but filter widget won't be initialized This way :(
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.
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:
It would be great to have setDefaultFilter().
Thanks !