Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here
Tutorial Free

Filament: Click on Widget - Auto-Update Table Filter

March 22, 2023
3 min read

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.

Enjoyed This Tutorial?

Get access to all premium tutorials, video and text courses, and exclusive Laravel resources. Join our community of 10,000+ developers.

Comments & Discussion

2
23sergej ✓ Link copied!

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

Am I missing something

PK
Povilas Korop ✓ Link copied!

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.

2
23sergej ✓ Link copied!

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? :)

PK
Povilas Korop ✓ Link copied!

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 :)

2
23sergej ✓ Link copied!

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

K
Kanny ✓ Link copied!

@23sergej This will work...

->extraAttributes([
'wire:click' => new HtmlString("$emit('filterUpdate', 'is_admin')")
])
SZ
sadegh zabeh ✓ Link copied!

in livewire V3: you can use this:

->extraAttributes([
'class' => 'cursor-pointer',
'wire:click' => "\$dispatch('filterUpdate', { filter: 'is_admin' })",
]),
SZ
sadegh zabeh ✓ Link copied!

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

PK
Povilas Korop ✓ Link copied!

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.

AH
Abu Hurairah ✓ Link copied!

Hi Povilas, this tip is gold. I've been looking for a way to do this for quite some time and couldnot find any reference in your https://filamentexamples.com/.

For some reason, I cant found this article when I search using your search page here, but able to stumble into it by using google search.

Although it is for Filament 2, just need to do minor changes to make it work with Filament 3. It would be good if you could refresh the article and make it available for others too.

Thanks.

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.