Toggle Filtering

Hello !
I’m building a simple subpage filtering menu with tags, and I was wondering if there was an easy way to “Toggle” the filtering. For example: on the first click the tag is active, and if you select It again the filtering is gone and all the subpages are displayed.

Here is my tag snippet

<?php foreach($topics as $topic): ?>
    	<a class="tag" href="<?= url($page->url(), ['params' => ['topic' => urlencode($topic)]]) ?>" ><?= html($topic) ?><span class="em-space"></span></a>
<?php endforeach ?>

And the Controller

<?php

return function ($kirby, $pages, $page) {

  $projets = $page->children()->filterBy('template', 'projet');
  $topics = $projets->pluck('topics', ',', true);
   
  if($topic = urldecode(param('topic'))) {
    $projets = $projets->filterBy('topics', $topic, ',');
  }

return compact('topics', 'topic', 'projets');

};

Thank you !

Yes, that would be possible with a bit of JavaScript. It would probably be easier and more user friendly to simply add an “All topics” link, like for example on this page: Cookbook | Kirby CMS

Thank you ! I’ll go with javascript then. I thought maybe a kind of if parameter on the ‘params’ might do the trick.

That could work as well, if you check if the param is already set.