Filament: Click on Widget - Auto-Update Table Filter

Let me show you one Filament "trick". In your resource list, you might have some stats widgets. What if you could click on them and set the tables filter according to the widget?

filament widgets

See this table with the widgets above? In this example, we will make the Total admins and Total managers widgets clickable.

The widgets are very simple. Admin and Manager are just boolean fields in the users table is_admin and is_manager respectively. The starter widget would look like this:

app/Filament/Resources/UserResource/Widgets/UserOverview.php:

use App\Models\User;
use Filament\Widgets\StatsOverviewWidget\Card;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
 
class UserOverview extends BaseWidget
{
protected function getCards(): array
{
return [
Card::make('Total users', User::count()),
Card::make('Total admins', User::where('is_admin', true)->count()),
Card::make('Total managers', User::where('is_manager', true)->count()),
];
}
}

First, let's make cards clickable. For this, we need to add the extraAttributes() method to the card.

class UserOverview extends BaseWidget
{
protected function getCards(): array
{
return [
Card::make('Total users', User::count()),
Card::make('Total admins', User::where('is_admin', true)->count())
->extraAttributes([
'wire:click' => '$emit("filterUpdate", "is_admin")',
'class' => 'transition hover:text-primary-500 cursor-pointer',
]),
Card::make('Total managers', User::where('is_manager', true)->count())
->extraAttributes([
'wire:click' => '$emit("filterUpdate", "is_manager")',
'class' => 'transition hover:text-primary-500 cursor-pointer',
]),
];
}
}

So, what are we doing here?

  • First, we add the wire:click element which when clicked will fire the event filterUpdate and will send which filter to update.
  • Second, we add CSS classes, so that the user when will hover the mouse over the widget, will see that it has some action.

Now, we need to add an event listener to the ListRecords page.

app/Filament/Resources/UserResource/Pages/ListUsers.php:

class ListUsers extends ListRecords
{
protected static string $resource = UserResource::class;
 
protected $listeners = ['filterUpdate' => 'updateTableFilters'];
 
protected function getActions(): array
{
return [
Actions\CreateAction::make(),
];
}
 
protected function getHeaderWidgets(): array
{
return UserResource::getWidgets();
}
 
public function updateTableFilters(string $filter): void
{
$this->tableFilters[$filter]['isActive'] = true;
}
}

Now what we added here:

  • First, event listener. When this component picks up the filterUpdate event, it will call the updateTableFilters() method.
  • Next, in the updateTableFilters() method we set table filter.

Now if you will click on a widget, you will see that the filter is applied.

active filter


If you want more Filament examples, you can find more real-life projects on our FilamentExamples.com.

avatar

I'm getting an error in browser console: module.esm.js:425 Uncaught ReferenceError: $wire is not defined

Am I missing something

avatar

Usually $wire is not defined means that Livewire is not installed on your system. So probably you have installed Filament/Livewire in some non-standard way.

avatar

Hey Povilas,

Thanks for quick answer. I found the problem, for some reason HTML escape is applied to the extra attributes values.

I'm adding following code on the backend

->extraAttributes([
		'wire:click' => '$emit("filterUpdate", "is_admin")'
])

And my HTML in inspect element is following: wire:click="$emit("filterUpdate", "is_admin")"

As you can see it converts " to " and I get Javascript error Uncaught SyntaxError: Unexpected token '&'. And this also leads to the ReferenceError: $wire is not defined error

P. S. I've tried using single quotes, but it didn't helped. Any ideas? :)

avatar

I haven't tried to use extraAttributes with wire:click like this, I'm not even sure it would work, to be honest. But I may be wrong, just haven't tried. Sorry, no ideas from me :)

avatar

Thanks for your reply. Laravel Daily is the best! ;)

avatar

@23sergej This will work...

->extraAttributes([
		'wire:click' => new HtmlString("$emit('filterUpdate', 'is_admin')")
])
avatar

in livewire V3: you can use this:

->extraAttributes([
          'class' => 'cursor-pointer',
          'wire:click' => "\$dispatch('filterUpdate', { filter: 'is_admin' })",
        ]),
avatar

Hi Povilas,

I am using this code in filament 2.

but this code is not working in filament 3

Error Code:

Alpine Expression Error: Invalid or unexpected token

Expression: "$wire.$emit

avatar

With Filament 3 it's a totally different syntax, because Livewire 3 doesn't have the emit event anymore.

But Filament 3 has that auto-refresh feature out-of-the-box, from what I remember, read the docs about Widgets and Tables.

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 58 courses (1056 lessons, total 44 h 09 min)
  • 78 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent Premium Tutorials