Show amount of results for each filter next to it


Hi all,

As you can see in the screenshot I have a list of checkbox filters. They work, but I would like to display the amount of resources that are found for that filter within the active filtering. Anyone any idea on how to do this? I use the ResourceBundle & Gridbundle in an custom SF Project . Thanks in advance!

This is an example of how I want it (this is HTML Template, so not dynamic).



The best way I can think of is getting the result set, transforming it into a Doctrine ArrayCollection if it isn’t already and for each filter build a Criteria and use the matching method to to apply it.
I would do it the following way:

  • Implement a new Sylius\Component\Grid\Data\ExpressionBuilderInterface which will use Doctrine\Common\Collections\ExpressionBuilder,
  • Implement a new Sylius\Component\Grid\Data\DataSourceInterface, instantiate the expression builder previously created, instantiate a new Doctrine\Common\Collections\Criteria, in restrict method use Criteria andWhere and orWhere, in getData use Doctrine\Common\Collections\ArrayCollection::matching with the current Criteria, then reset the Criteria.
  • Implement a new Sylius\Component\Grid\Filtering\FiltersApplicatorInterface which after applying each filter will call $dataSource->getData() and store the result (being the filtered collection or just it’s count)
  • A new Sylius\Component\Grid\Data\DataProviderInterface which will at least skip the sorting

You can run the thing in an event listener on resource index action.
Probably the easiest way to store the results is to create a service which will store them per filter (by name) and per grid (by code).
And the easiest way to display the counters is through filter templates by calling from there a twig function or filter which will return the count (or collection) using the service mentioned above.

You could save the collections instead of only the count and pass some resource identifiers to the view layer, then write some JS which will quickly apply the filter while making the actual filter request via AJAX.

Relevant stuff:

  • Sylius\Bundle\ResourceBundle\Controller\ResourceController::indexAction
  • Sylius\Bundle\ResourceBundle\Controller\ResourcesCollectionProvider::get
  • Sylius\Bundle\ResourceBundle\Grid\Controller\ResourcesResolver::getResources
  • Sylius\Bundle\ResourceBundle\Grid\View\ResourceGridViewFactory::create
  • Sylius\Bundle\GridBundle\Templating\Helper\GridHelper
  • Sylius\Bundle\GridBundle\Renderer\TwigGridRenderer::renderFilter

For templates see: SyliusUiBundle/Resources/views/Grid